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 1089 2004-05-27 15:41:37Z 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)
226 java_objectheader *vmo;
228 /* vt = class_java_lang_Class->vftbl; */
230 if (!c->classvftbl) {
231 c->classvftbl = true;
233 /* copy_vftbl(&newtbl, vt);
234 newtbl->class = c->header.vftbl->class;
235 newtbl->baseval = c->header.vftbl->baseval;
236 newtbl->diffval = c->header.vftbl->diffval;
237 c->header.vftbl = newtbl;*/
239 c->header.vftbl = class_java_lang_Class->vftbl;
241 method_vmclass_init =
242 class_findmethod(class_java_lang_VMClass,
243 utf_new_char("<init>"),
244 utf_new_char("(Lgnu/classpath/RawData;)V"));
246 if (method_vmclass_init == 0) {
247 class_showmethods(class_java_lang_VMClass);
248 panic("Needed class initializer for VMClass could not be found");
251 vmo = builtin_new(class_java_lang_VMClass);
254 panic("Error while creating instance of java/lang/VMClass");
256 asm_calljavafunction(method_vmclass_init, vmo, c, NULL, NULL);
258 c->vmClass = (java_lang_VMClass *) vmo;
260 setfield_critical(class_java_lang_Class,vmo,"vmClass", "Ljava/lang/VMClass;", jobject, (jobject) class_java_lang_Class /*this*/);
268 /*************************** include native methods ***************************/
271 #include "nat/GdkGraphics.c"
272 #include "nat/GtkComponentPeer.c"
273 #include "nat/GdkPixbufDecoder.c"
274 #include "nat/GtkScrollPanePeer.c"
275 #include "nat/GtkFileDialogPeer.c"
276 #include "nat/GtkLabelPeer.c"
280 /************************** tables for methods ********************************/
285 /* table for locating native methods */
286 static struct nativeref {
294 #include "nativetable.hh"
299 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
301 /* table for fast string comparison */
302 static struct nativecompref {
308 } nativecomptable [NATIVETABLESIZE];
310 /* string comparsion table initialized */
311 static bool nativecompdone = false;
314 /******************************************************************************/
315 /******************************************************************************/
316 #include "natcalls.h"
318 /* string call comparison table initialized */
320 /******************************************************************************/
321 /******************************************************************************/
323 /* init_system_exceptions *****************************************************
325 load, link and compile exceptions used in the system
327 *******************************************************************************/
329 void init_system_exceptions()
333 /* java/lang/Throwable */
335 class_java_lang_Throwable =
336 class_new(utf_new_char(string_java_lang_Throwable));
337 class_load(class_java_lang_Throwable);
338 class_link(class_java_lang_Throwable);
339 compile_all_class_methods(class_java_lang_Throwable);
341 /* java/lang/VMThrowable */
343 c = class_new(utf_new_char(string_java_lang_VMThrowable));
346 compile_all_class_methods(c);
348 /* java/lang/ClassFormatError */
350 c = class_new(utf_new_char(string_java_lang_ClassFormatError));
353 compile_all_class_methods(c);
355 /* java/lang/Error */
357 c = class_new(utf_new_char(string_java_lang_Error));
360 compile_all_class_methods(c);
362 /* java/lang/Exception */
364 c = class_new(utf_new_char(string_java_lang_Exception));
367 compile_all_class_methods(c);
369 /* java/lang/IncompatibleClassChangeError */
371 c = class_new(utf_new_char(string_java_lang_IncompatibleClassChangeError));
374 compile_all_class_methods(c);
376 /* java/lang/LinkageError */
378 c = class_new(utf_new_char(string_java_lang_LinkageError));
381 compile_all_class_methods(c);
383 /* java/lang/NoClassDefFoundError */
385 c = class_new(utf_new_char(string_java_lang_NoClassDefFoundError));
388 compile_all_class_methods(c);
390 /* java/lang/NoSuchFieldError */
392 c = class_new(utf_new_char(string_java_lang_NoSuchFieldError));
395 compile_all_class_methods(c);
397 /* java/lang/NoSuchMethodError */
399 c = class_new(utf_new_char(string_java_lang_NoSuchMethodError));
402 compile_all_class_methods(c);
404 /* java/lang/OutOfMemoryError */
406 c = class_new(utf_new_char(string_java_lang_OutOfMemoryError));
409 compile_all_class_methods(c);
411 /* java/lang/VerifyError */
413 c = class_new(utf_new_char(string_java_lang_VerifyError));
416 compile_all_class_methods(c);
418 /* java/lang/VirtualMachineError */
420 c = class_new(utf_new_char(string_java_lang_VirtualMachineError));
423 compile_all_class_methods(c);
427 void compile_all_class_methods(classinfo *c)
431 for (i = 0; i < c->methodscount; i++) {
432 (void) jit_compile(&(c->methods[i]));
437 void throw_exception_exit()
439 java_objectheader *xptr;
444 xptr = *exceptionptr;
446 /* clear exception, because we are calling jit code again */
447 *exceptionptr = NULL;
449 fprintf(stderr, "Exception in thread \"main\" ");
452 c = xptr->vftbl->class;
454 /* ts = class_resolveclassmethod(c, */
455 /* utf_new_char("toString"), */
456 /* utf_new_char("()Ljava/lang/String;"), */
457 /* class_java_lang_Object, */
461 /* panic("internal error"); */
464 /* tostring = asm_calljavafunction(ts, xptr, NULL, NULL, NULL); */
466 /* utf_display(javastring_toutf(tostring, false)); */
468 pss = class_resolveclassmethod(c,
469 utf_new_char("printStackTrace"),
471 class_java_lang_Object,
474 /* print the stacktrace */
476 asm_calljavafunction(pss, xptr, NULL, NULL, NULL);
478 /* this normally means, we are EXTREMLY out of memory, but may be
479 any other exception */
481 utf_fprint_classname(stderr, c->name);
482 fprintf(stderr, "\n");
486 utf_fprint_classname(stderr, c->name);
487 fprintf(stderr, ": printStackTrace()V not found!\n");
498 void throw_cacao_exception_exit(char *exception, char *message)
504 len = strlen(exception);
505 tmp = MNEW(char*, len);
506 strncpy(tmp, exception, len);
508 /* convert to classname */
510 for (i = len - 1; i >= 0; i--) {
511 if (tmp[i] == '/') tmp[i] = '.';
514 fprintf(stderr, "Exception in thread \"main\" %s", tmp);
516 MFREE(tmp, char*, len);
518 if (strlen(message) > 0)
519 fprintf(stderr, ": %s", message);
521 fprintf(stderr, "\n");
529 java_objectheader *new_exception(char *classname)
531 classinfo *c = class_new(utf_new_char(classname));
533 return native_new_and_init(c);
536 java_objectheader *new_exception_message(char *classname, char *message)
538 classinfo *c = class_new(utf_new_char(classname));
540 return native_new_and_init_string(c, javastring_new_char(message));
544 java_objectheader *new_exception_throwable(char *classname, java_lang_Throwable *throwable)
546 classinfo *c = class_new(utf_new_char(classname));
548 return native_new_and_init_throwable(c, throwable);
552 java_objectheader *new_exception_utfmessage(char *classname, utf *message)
554 classinfo *c = class_new(utf_new_char(classname));
556 return native_new_and_init_string(c, javastring_new(message));
560 java_objectheader *new_exception_javastring(char *classname, java_lang_String *message)
562 classinfo *c = class_new(utf_new_char(classname));
564 return native_new_and_init_string(c, message);
568 java_objectheader *new_exception_int(char *classname, s4 i)
570 classinfo *c = class_new(utf_new_char(classname));
572 return native_new_and_init_int(c, i);
576 /*********************** function: native_loadclasses **************************
578 load classes required for native methods
580 *******************************************************************************/
582 void native_loadclasses()
584 static int classesLoaded = 0; /*temporary hack JoWenn*/
591 class_java_lang_Cloneable =
592 class_new(utf_new_char("java/lang/Cloneable"));
593 class_load(class_java_lang_Cloneable);
594 class_link(class_java_lang_Cloneable);
596 class_java_lang_Class =
597 class_new(utf_new_char("java/lang/Class"));
598 class_load(class_java_lang_Class);
599 class_link(class_java_lang_Class);
601 class_java_lang_VMClass =
602 class_new(utf_new_char("java/lang/VMClass"));
603 class_load(class_java_lang_VMClass);
604 class_link(class_java_lang_VMClass);
606 class_java_lang_ClassLoader =
607 class_new(utf_new_char("java/lang/ClassLoader"));
608 class_load(class_java_lang_ClassLoader);
609 class_link(class_java_lang_ClassLoader);
611 /* load classes for wrapping primitive types */
612 class_java_lang_Double = class_new(utf_new_char("java/lang/Double"));
613 class_load(class_java_lang_Double);
614 class_link(class_java_lang_Double);
616 class_java_lang_Float = class_new(utf_new_char("java/lang/Float"));
617 class_load(class_java_lang_Float);
618 class_link(class_java_lang_Float);
620 class_java_lang_Character = class_new(utf_new_char("java/lang/Character"));
621 class_load(class_java_lang_Character);
622 class_link(class_java_lang_Character);
624 class_java_lang_Integer = class_new(utf_new_char("java/lang/Integer"));
625 class_load(class_java_lang_Integer);
626 class_link(class_java_lang_Integer);
628 class_java_lang_Long = class_new(utf_new_char("java/lang/Long"));
629 class_load(class_java_lang_Long);
630 class_link(class_java_lang_Long);
632 class_java_lang_Byte = class_new(utf_new_char("java/lang/Byte"));
633 class_load(class_java_lang_Byte);
634 class_link(class_java_lang_Byte);
636 class_java_lang_Short = class_new(utf_new_char("java/lang/Short"));
637 class_load(class_java_lang_Short);
638 class_link(class_java_lang_Short);
640 class_java_lang_Boolean = class_new(utf_new_char("java/lang/Boolean"));
641 class_load(class_java_lang_Boolean);
642 class_link(class_java_lang_Boolean);
644 class_java_lang_Void = class_new(utf_new_char("java/lang/Void"));
645 class_load(class_java_lang_Void);
646 class_link(class_java_lang_Void);
650 /*****************************************************************************
652 create systemclassloader object and initialize instance fields
654 ******************************************************************************/
656 void init_systemclassloader()
658 log_text("init_systemclassloader");
659 if (!SystemClassLoader) {
660 native_loadclasses();
661 log_text("Initializing new system class loader");
662 /* create object and call initializer */
663 SystemClassLoader = (java_lang_ClassLoader *) native_new_and_init(class_new(utf_new_char("gnu/java/lang/SystemClassLoader")));
665 /* systemclassloader has no parent */
666 SystemClassLoader->parent = NULL;
667 SystemClassLoader->initialized = true;
669 log_text("leaving system class loader");
673 /********************* function: native_setclasspath **************************/
675 void native_setclasspath(char *path)
677 /* set searchpath for classfiles */
682 /*********************** Function: native_findfunction *************************
684 Looks up a method (must have the same class name, method name, descriptor
685 and 'static'ness) and returns a function pointer to it.
686 Returns: function pointer or NULL (if there is no such method)
688 Remark: For faster operation, the names/descriptors are converted from C
689 strings to Unicode the first time this function is called.
691 *******************************************************************************/
693 functionptr native_findfunction(utf *cname, utf *mname,
694 utf *desc, bool isstatic)
697 /* entry of table for fast string comparison */
698 struct nativecompref *n;
699 /* for warning message if no function is found */
703 isstatic = isstatic ? true : false;
705 if (!nativecompdone) {
706 for (i = 0; i < NATIVETABLESIZE; i++) {
707 nativecomptable[i].classname =
708 utf_new_char(nativetable[i].classname);
709 nativecomptable[i].methodname =
710 utf_new_char(nativetable[i].methodname);
711 nativecomptable[i].descriptor =
712 utf_new_char(nativetable[i].descriptor);
713 nativecomptable[i].isstatic =
714 nativetable[i].isstatic;
715 nativecomptable[i].func =
718 nativecompdone = true;
723 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
725 buffer = MNEW(char, buffer_len);
727 strcpy(buffer, "searching matching function in native table:");
728 utf_sprint(buffer+strlen(buffer), mname);
729 strcpy(buffer+strlen(buffer), ": ");
730 utf_sprint(buffer+strlen(buffer), desc);
731 strcpy(buffer+strlen(buffer), " for class ");
732 utf_sprint(buffer+strlen(buffer), cname);
736 MFREE(buffer, char, buffer_len);
739 for (i = 0; i < NATIVETABLESIZE; i++) {
740 n = &(nativecomptable[i]);
742 if (cname == n->classname && mname == n->methodname &&
743 desc == n->descriptor && isstatic == n->isstatic)
747 if (cname == n->classname && mname == n->methodname ) log_text("static and descriptor mismatch");
751 utf_strlen(n->classname) + utf_strlen(n->methodname) + utf_strlen(n->descriptor) + 64;
753 buffer = MNEW(char, buffer_len);
755 strcpy(buffer, "comparing with:");
756 utf_sprint(buffer+strlen(buffer), n->methodname);
757 strcpy (buffer+strlen(buffer), ": ");
758 utf_sprint(buffer+strlen(buffer), n->descriptor);
759 strcpy(buffer+strlen(buffer), " for class ");
760 utf_sprint(buffer+strlen(buffer), n->classname);
764 MFREE(buffer, char, buffer_len);
771 /* no function was found, display warning */
774 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
776 buffer = MNEW(char, buffer_len);
778 strcpy(buffer, "warning: native function ");
779 utf_sprint(buffer + strlen(buffer), mname);
780 strcpy(buffer + strlen(buffer), ": ");
781 utf_sprint(buffer + strlen(buffer), desc);
782 strcpy(buffer + strlen(buffer), " not found in class ");
783 utf_sprint(buffer + strlen(buffer), cname);
787 MFREE(buffer, char, buffer_len);
791 /* keep compiler happy */
796 /********************** function: javastring_new *******************************
798 creates a new object of type java/lang/String with the text of
799 the specified utf8-string
801 return: pointer to the string or NULL if memory is exhausted.
803 *******************************************************************************/
805 java_lang_String *javastring_new(utf *u)
807 char *utf_ptr = u->text; /* current utf character in utf string */
808 int utflength = utf_strlen(u); /* length of utf-string if uncompressed */
809 java_lang_String *s; /* result-string */
813 s = (java_lang_String *) builtin_new(class_java_lang_String);
814 a = builtin_newarray_char(utflength);
816 /* javastring or character-array could not be created */
820 /* decompress utf-string */
821 for (i = 0; i < utflength; i++)
822 a->data[i] = utf_nextu2(&utf_ptr);
824 /* set fields of the javastring-object */
827 s->count = utflength;
833 /********************** function: javastring_new_char **************************
835 creates a new java/lang/String object which contains the convertet
836 C-string passed via text.
838 return: the object pointer or NULL if memory is exhausted.
840 *******************************************************************************/
842 java_lang_String *javastring_new_char(char *text)
845 s4 len = strlen(text); /* length of the string */
846 java_lang_String *s; /* result-string */
849 s = (java_lang_String *) builtin_new(class_java_lang_String);
850 a = builtin_newarray_char(len);
852 /* javastring or character-array could not be created */
857 for (i = 0; i < len; i++)
858 a->data[i] = text[i];
860 /* set fields of the javastring-object */
869 /************************* function javastring_tochar **************************
871 converts a Java string into a C string.
873 return: pointer to C string
875 Caution: every call of this function overwrites the previous string !!!
877 *******************************************************************************/
879 static char stringbuffer[MAXSTRINGSIZE];
881 char *javastring_tochar(java_objectheader *so)
883 java_lang_String *s = (java_lang_String *) so;
895 if (s->count > MAXSTRINGSIZE)
898 for (i = 0; i < s->count; i++)
899 stringbuffer[i] = a->data[s->offset + i];
901 stringbuffer[i] = '\0';
907 /****************** function class_findfield_approx ****************************
909 searches in 'classinfo'-structure for a field with the
912 *******************************************************************************/
914 fieldinfo *class_findfield_approx(classinfo *c, utf *name)
918 for (i = 0; i < c->fieldscount; i++) {
919 /* compare field names */
920 if ((c->fields[i].name == name))
921 return &(c->fields[i]);
924 /* field was not found, raise exception */
925 *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
931 s4 class_findfield_index_approx(classinfo *c, utf *name)
935 for (i = 0; i < c->fieldscount; i++) {
936 /* compare field names */
937 if ((c->fields[i].name == name))
941 /* field was not found, raise exception */
942 *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
948 /********************** function: native_new_and_init *************************
950 Creates a new object on the heap and calls the initializer.
951 Returns the object pointer or NULL if memory is exhausted.
953 *******************************************************************************/
955 java_objectheader *native_new_and_init(classinfo *c)
958 java_objectheader *o;
961 return *exceptionptr;
963 o = builtin_new(c); /* create object */
968 /* find initializer */
970 m = class_findmethod(c,
971 utf_new_char("<init>"),
972 utf_new_char("()V"));
974 if (!m) { /* initializer not found */
976 char logtext[MAXLOGTEXT];
977 sprintf(logtext, "Warning: class has no instance-initializer: ");
978 utf_sprint_classname(logtext + strlen(logtext), c->name);
984 /* call initializer */
986 asm_calljavafunction(m, o, NULL, NULL, NULL);
992 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
995 java_objectheader *o;
998 return *exceptionptr;
1000 o = builtin_new(c); /* create object */
1005 /* find initializer */
1007 m = class_findmethod(c,
1008 utf_new_char("<init>"),
1009 utf_new_char("(Ljava/lang/String;)V"));
1011 if (!m) { /* initializer not found */
1013 char logtext[MAXLOGTEXT];
1014 sprintf(logtext, "Warning: class has no instance-initializer: ");
1015 utf_sprint_classname(logtext + strlen(logtext), c->name);
1021 /* call initializer */
1023 asm_calljavafunction(m, o, s, NULL, NULL);
1029 java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
1032 java_objectheader *o;
1035 return *exceptionptr;
1037 o = builtin_new(c); /* create object */
1039 if (!o) return NULL;
1041 /* find initializer */
1043 m = class_findmethod(c,
1044 utf_new_char("<init>"),
1045 utf_new_char("(I)V"));
1047 if (!m) { /* initializer not found */
1049 char logtext[MAXLOGTEXT];
1050 sprintf(logtext, "Warning: class has no instance-initializer: ");
1051 utf_sprint_classname(logtext + strlen(logtext), c->name);
1057 /* call initializer */
1059 asm_calljavafunction(m, o, (void *) i, NULL, NULL);
1065 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t)
1068 java_objectheader *o;
1071 return *exceptionptr;
1073 o = builtin_new(c); /* create object */
1075 if (!o) return NULL;
1077 /* find initializer */
1079 m = class_findmethod(c,
1080 utf_new_char("<init>"),
1081 utf_new_char("(Ljava/lang/Throwable;)V"));
1083 if (!m) { /* initializer not found */
1085 char logtext[MAXLOGTEXT];
1086 sprintf(logtext, "Warning: class has no instance-initializer: ");
1087 utf_sprint_classname(logtext + strlen(logtext), c->name);
1093 /* call initializer */
1095 asm_calljavafunction(m, o, t, NULL, NULL);
1101 /******************** function: stringtable_update ****************************
1103 traverses the javastring hashtable and sets the vftbl-entries of
1104 javastrings which were temporarily set to NULL, because
1105 java.lang.Object was not yet loaded
1107 *******************************************************************************/
1109 void stringtable_update ()
1111 java_lang_String *js;
1113 literalstring *s; /* hashtable entry */
1116 for (i = 0; i < string_hash.size; i++) {
1117 s = string_hash.ptr[i];
1121 js = (java_lang_String *) s->string;
1123 if (!js || !js->value)
1124 /* error in hashtable found */
1125 panic("invalid literalstring in hashtable");
1129 if (!js->header.vftbl)
1130 /* vftbl of javastring is NULL */
1131 js->header.vftbl = class_java_lang_String->vftbl;
1133 if (!a->header.objheader.vftbl)
1134 /* vftbl of character-array is NULL */
1135 a->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
1137 /* follow link in external hash chain */
1145 /************************* function: u2_utflength ***************************
1147 returns the utf length in bytes of a u2 array
1149 *****************************************************************************/
1151 u4 u2_utflength(u2 *text, u4 u2_length)
1153 u4 result_len = 0; /* utf length in bytes */
1154 u2 ch; /* current unicode character */
1157 for (len = 0; len < u2_length; len++) {
1158 /* next unicode character */
1161 /* determine bytes required to store unicode character as utf */
1162 if (ch && (ch < 0x80))
1164 else if (ch < 0x800)
1174 /********************* function: utf_new_u2 ***********************************
1176 make utf symbol from u2 array,
1177 if isclassname is true '.' is replaced by '/'
1179 *******************************************************************************/
1181 utf *utf_new_u2(u2 *unicode_pos, u4 unicode_length, bool isclassname)
1183 char *buffer; /* memory buffer for unicode characters */
1184 char *pos; /* pointer to current position in buffer */
1185 u4 left; /* unicode characters left */
1186 u4 buflength; /* utf length in bytes of the u2 array */
1187 utf *result; /* resulting utf-string */
1190 /* determine utf length in bytes and allocate memory */
1191 /* printf("utf_new_u2: unicode_length=%d\n",unicode_length); */
1192 buflength = u2_utflength(unicode_pos, unicode_length);
1193 buffer = MNEW(char, buflength);
1195 /* memory allocation failed */
1197 printf("length: %d\n",buflength);
1198 log_text("utf_new_u2:buffer==NULL");
1204 for (i = 0; i++ < unicode_length; unicode_pos++) {
1205 /* next unicode character */
1206 u2 c = *unicode_pos;
1208 if ((c != 0) && (c < 0x80)) {
1211 if ((int) left < 0) break;
1212 /* convert classname */
1213 if (isclassname && c == '.')
1218 } else if (c < 0x800) {
1220 unsigned char high = c >> 6;
1221 unsigned char low = c & 0x3F;
1223 if ((int) left < 0) break;
1224 *pos++ = high | 0xC0;
1225 *pos++ = low | 0x80;
1229 char low = c & 0x3f;
1230 char mid = (c >> 6) & 0x3F;
1231 char high = c >> 12;
1233 if ((int) left < 0) break;
1234 *pos++ = high | 0xE0;
1235 *pos++ = mid | 0x80;
1236 *pos++ = low | 0x80;
1240 /* insert utf-string into symbol-table */
1241 result = utf_new(buffer,buflength);
1243 MFREE(buffer, char, buflength);
1249 /********************* function: javastring_toutf *****************************
1251 make utf symbol from javastring
1253 *******************************************************************************/
1255 utf *javastring_toutf(java_lang_String *string, bool isclassname)
1257 java_lang_String *str = (java_lang_String *) string;
1259 /* printf("javastring_toutf offset: %d, len %d\n",str->offset, str->count); */
1260 /* fflush(stdout); */
1262 return utf_new_u2(str->value->data + str->offset, str->count, isclassname);
1266 /********************* function: literalstring_u2 *****************************
1268 searches for the javastring with the specified u2-array in
1269 the string hashtable, if there is no such string a new one is
1272 if copymode is true a copy of the u2-array is made
1274 *******************************************************************************/
1276 java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset,
1279 literalstring *s; /* hashtable element */
1280 java_lang_String *js; /* u2-array wrapped in javastring */
1281 java_chararray *stringdata; /* copy of u2-array */
1286 //#define DEBUG_LITERALSTRING_U2
1287 #ifdef DEBUG_LITERALSTRING_U2
1288 printf("literalstring_u2: length=%d, offset=%d\n", length, offset);
1292 /* find location in hashtable */
1293 key = unicode_hashkey(a->data + offset, length);
1294 slot = key & (string_hash.size - 1);
1295 s = string_hash.ptr[slot];
1298 js = (java_lang_String *) s->string;
1300 if (length == js->count) {
1302 for (i = 0; i < length; i++) {
1303 if (a->data[offset + i] != js->value->data[i])
1307 /* string already in hashtable, free memory */
1309 lit_mem_free(a, sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10);
1311 #ifdef DEBUG_LITERALSTRING_U2
1312 printf("literalstring_u2: foundentry at %p\n", js);
1313 utf_display(javastring_toutf(js, 0));
1317 return (java_objectheader *) js;
1321 /* follow link in external hash chain */
1326 /* create copy of u2-array for new javastring */
1327 u4 arraysize = sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10;
1328 stringdata = lit_mem_alloc(arraysize);
1329 /* memcpy(stringdata, a, arraysize); */
1330 memcpy(&(stringdata->header), &(a->header), sizeof(java_arrayheader));
1331 memcpy(&(stringdata->data), &(a->data) + offset, sizeof(u2) * (length - 1) + 10);
1337 /* location in hashtable found, complete arrayheader */
1338 stringdata->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
1339 stringdata->header.size = length;
1341 /* create new javastring */
1342 js = LNEW(java_lang_String);
1344 /* js->header.vftbl = class_java_lang_String->vftbl; */
1345 js->header.vftbl = class_load(class_new(utf_new_char("java/lang/String")))->vftbl;
1346 js->value = stringdata;
1350 /* create new literalstring */
1351 s = NEW(literalstring);
1352 s->hashlink = string_hash.ptr[slot];
1353 s->string = (java_objectheader *) js;
1354 string_hash.ptr[slot] = s;
1356 /* update numbe of hashtable entries */
1357 string_hash.entries++;
1359 /* reorganization of hashtable */
1360 if (string_hash.entries > (string_hash.size * 2)) {
1361 /* reorganization of hashtable, average length of
1362 the external chains is approx. 2 */
1366 hashtable newhash; /* the new hashtable */
1368 /* create new hashtable, double the size */
1369 init_hashtable(&newhash, string_hash.size * 2);
1370 newhash.entries = string_hash.entries;
1372 /* transfer elements to new hashtable */
1373 for (i = 0; i < string_hash.size; i++) {
1374 s = string_hash.ptr[i];
1376 literalstring *nexts = s->hashlink;
1377 js = (java_lang_String *) s->string;
1378 slot = unicode_hashkey(js->value->data, js->count) & (newhash.size - 1);
1380 s->hashlink = newhash.ptr[slot];
1381 newhash.ptr[slot] = s;
1383 /* follow link in external hash chain */
1388 /* dispose old table */
1389 MFREE(string_hash.ptr, void*, string_hash.size);
1390 string_hash = newhash;
1393 #ifdef DEBUG_LITERALSTRING_U2
1394 printf("literalstring_u2: newly created at %p\n", js);
1395 utf_display(javastring_toutf(js, 0));
1400 return (java_objectheader *) js;
1404 /******************** Function: literalstring_new *****************************
1406 creates a new javastring with the text of the utf-symbol
1407 and inserts it into the string hashtable
1409 *******************************************************************************/
1411 java_objectheader *literalstring_new(utf *u)
1413 char *utf_ptr = u->text; /* pointer to current unicode character in utf string */
1414 u4 utflength = utf_strlen(u); /* length of utf-string if uncompressed */
1415 java_chararray *a; /* u2-array constructed from utf string */
1418 /* allocate memory */
1419 a = lit_mem_alloc(sizeof(java_chararray) + sizeof(u2) * (utflength - 1) + 10);
1421 /* convert utf-string to u2-array */
1422 for (i = 0; i < utflength; i++)
1423 a->data[i] = utf_nextu2(&utf_ptr);
1425 return literalstring_u2(a, utflength, 0, false);
1429 /********************** function: literalstring_free **************************
1431 removes a javastring from memory
1433 ******************************************************************************/
1435 void literalstring_free(java_objectheader* sobj)
1437 java_lang_String *s = (java_lang_String *) sobj;
1438 java_chararray *a = s->value;
1440 /* dispose memory of java.lang.String object */
1441 LFREE(s, java_lang_String);
1443 /* dispose memory of java-characterarray */
1444 LFREE(a, sizeof(java_chararray) + sizeof(u2) * (a->header.size - 1)); /* +10 ?? */
1448 void copy_vftbl(vftbl **dest, vftbl *src)
1452 /* XXX this kind of copying does not work (in the general
1453 * case). The interface tables would have to be copied, too. I
1454 * don't see why we should make a copy anyway. -Edwin
1456 *dest = mem_alloc(sizeof(vftbl) + sizeof(methodptr)*(src->vftbllength-1));
1457 memcpy(*dest, src, sizeof(vftbl) - sizeof(methodptr));
1458 memcpy(&(*dest)->table, &src->table, src->vftbllength * sizeof(methodptr));
1463 /******************************************************************************************
1465 creates method signature (excluding return type) from array of
1466 class-objects representing the parameters of the method
1468 *******************************************************************************************/
1471 utf *create_methodsig(java_objectarray* types, char *retType)
1473 char *buffer; /* buffer for building the desciptor */
1474 char *pos; /* current position in buffer */
1475 utf *result; /* the method signature */
1476 u4 buffer_size = 3; /* minimal size=3: room for parenthesis and returntype */
1479 if (!types) return NULL;
1481 /* determine required buffer-size */
1482 for (i = 0; i < types->header.size; i++) {
1483 classinfo *c = (classinfo *) types->data[i];
1484 buffer_size = buffer_size + c->name->blength + 2;
1487 if (retType) buffer_size += strlen(retType);
1489 /* allocate buffer */
1490 buffer = MNEW(u1, buffer_size);
1493 /* method-desciptor starts with parenthesis */
1496 for (i = 0; i < types->header.size; i++) {
1499 /* current argument */
1500 classinfo *c = (classinfo *) types->data[i];
1502 /* current position in utf-text */
1503 char *utf_ptr = c->name->text;
1505 /* determine type of argument */
1506 if ((ch = utf_nextu2(&utf_ptr)) == '[') {
1508 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1509 *pos++ = *utf_ptr; /* copy text */
1513 /* check for primitive types */
1514 for (j = 0; j < PRIMITIVETYPE_COUNT; j++) {
1515 char *utf_pos = utf_ptr - 1;
1516 char *primitive = primitivetype_table[j].wrapname;
1519 while (utf_pos < utf_end(c->name)) {
1520 if (*utf_pos++ != *primitive++) goto nomatch;
1523 /* primitive type found */
1524 *pos++ = primitivetype_table[j].typesig;
1531 /* no primitive type and no arrayclass, so must be object */
1535 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1549 for (i = 0; i < strlen(retType); i++) {
1550 *pos++ = retType[i];
1554 /* create utf-string */
1555 result = utf_new(buffer, (pos - buffer));
1556 MFREE(buffer, u1, buffer_size);
1562 /******************************************************************************************
1564 retrieve the next argument or returntype from a descriptor
1565 and return the corresponding class
1567 *******************************************************************************************/
1569 classinfo *get_type(char **utf_ptr,char *desc_end, bool skip)
1571 classinfo *c = class_from_descriptor(*utf_ptr,desc_end,utf_ptr,
1572 (skip) ? CLASSLOAD_SKIP : CLASSLOAD_LOAD);
1575 panic("illegal descriptor");
1577 if (skip) return NULL;
1579 use_class_as_object(c);
1584 /******************************************************************************************
1586 use the descriptor of a method to generate a java/lang/Class array
1587 which contains the classes of the parametertypes of the method
1589 *******************************************************************************************/
1591 java_objectarray* get_parametertypes(methodinfo *m)
1593 utf *descr = m->descriptor; /* method-descriptor */
1594 char *utf_ptr = descr->text; /* current position in utf-text */
1595 char *desc_end = utf_end(descr); /* points behind utf string */
1596 java_objectarray* result;
1597 int parametercount = 0;
1601 utf_nextu2(&utf_ptr);
1603 /* determine number of parameters */
1604 while ( *utf_ptr != ')' ) {
1605 get_type(&utf_ptr,desc_end,true);
1609 /* create class-array */
1610 result = builtin_anewarray(parametercount, class_java_lang_Class);
1612 utf_ptr = descr->text;
1613 utf_nextu2(&utf_ptr);
1615 /* get returntype classes */
1616 for (i = 0; i < parametercount; i++)
1617 result->data[i] = (java_objectheader *) get_type(&utf_ptr,desc_end, false);
1626 /******************************************************************************************
1628 get the exceptions which can be thrown by a method
1630 *******************************************************************************************/
1632 java_objectarray* get_exceptiontypes(methodinfo *m) {
1633 u2 exccount=m->thrownexceptionscount;
1635 java_objectarray *result;
1636 /* create class-array */
1637 result = builtin_anewarray(exccount, class_java_lang_Class);
1638 for (i=0;i<exccount;i++) {
1639 java_objectheader *oh=(java_objectheader*)(m->thrownexceptions[i]);
1640 use_class_as_object(oh);
1650 /******************************************************************************************
1652 get the returntype class of a method
1654 *******************************************************************************************/
1656 classinfo *get_returntype(methodinfo *m)
1658 char *utf_ptr; /* current position in utf-text */
1659 char *desc_end; /* points behind utf string */
1660 utf *desc = m->descriptor; /* method-descriptor */
1662 utf_ptr = desc->text;
1663 desc_end = utf_end(desc);
1665 /* ignore parametertypes */
1666 while ((utf_ptr<desc_end) && utf_nextu2(&utf_ptr)!=')')
1669 return get_type(&utf_ptr,desc_end, false);
1673 /*****************************************************************************/
1674 /*****************************************************************************/
1677 /*--------------------------------------------------------*/
1678 void printNativeCall(nativeCall nc) {
1681 printf("\n%s's Native Methods call:\n",nc.classname); fflush(stdout);
1682 for (i=0; i<nc.methCnt; i++) {
1683 printf("\tMethod=%s %s\n",nc.methods[i].methodname, nc.methods[i].descriptor);fflush(stdout);
1685 for (j=0; j<nc.callCnt[i]; j++) {
1686 printf("\t\t<%i,%i>aCalled = %s %s %s\n",i,j,
1687 nc.methods[i].methodCalls[j].classname,
1688 nc.methods[i].methodCalls[j].methodname,
1689 nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1692 printf("-+++++--------------------\n");fflush(stdout);
1695 /*--------------------------------------------------------*/
1696 void printCompNativeCall(nativeCompCall nc) {
1698 printf("printCompNativeCall BEGIN\n");fflush(stdout);
1699 printf("\n%s's Native Comp Methods call:\n",nc.classname->text);fflush(stdout);
1700 utf_display(nc.classname); fflush(stdout);
1702 for (i=0; i<nc.methCnt; i++) {
1703 printf("\tMethod=%s %s\n",nc.methods[i].methodname->text,nc.methods[i].descriptor->text);fflush(stdout);
1704 utf_display(nc.methods[i].methodname); fflush(stdout);
1705 utf_display(nc.methods[i].descriptor);fflush(stdout);
1706 printf("\n");fflush(stdout);
1708 for (j=0; j<nc.callCnt[i]; j++) {
1709 printf("\t\t<%i,%i>bCalled = ",i,j);fflush(stdout);
1710 utf_display(nc.methods[i].methodCalls[j].classname);fflush(stdout);
1711 utf_display(nc.methods[i].methodCalls[j].methodname); fflush(stdout);
1712 utf_display(nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1713 printf("\n");fflush(stdout);
1716 printf("---------------------\n");fflush(stdout);
1720 /*--------------------------------------------------------*/
1721 classMeth findNativeMethodCalls(utf *c, utf *m, utf *d )
1734 /*--------------------------------------------------------*/
1735 nativeCall* findNativeClassCalls(char *aclassname ) {
1738 for (i=0;i<NATIVECALLSSIZE; i++) {
1739 /* convert table to utf later to speed up search */
1740 if (strcmp(nativeCalls[i].classname, aclassname) == 0)
1741 return &nativeCalls[i];
1746 /*--------------------------------------------------------*/
1747 /*--------------------------------------------------------*/
1748 void utfNativeCall(nativeCall nc, nativeCompCall *ncc) {
1752 ncc->classname = utf_new_char(nc.classname);
1753 ncc->methCnt = nc.methCnt;
1755 for (i=0; i<nc.methCnt; i++) {
1756 ncc->methods[i].methodname = utf_new_char(nc.methods[i].methodname);
1757 ncc->methods[i].descriptor = utf_new_char(nc.methods[i].descriptor);
1758 ncc->callCnt[i] = nc.callCnt[i];
1760 for (j=0; j<nc.callCnt[i]; j++) {
1762 ncc->methods[i].methodCalls[j].classname = utf_new_char(nc.methods[i].methodCalls[j].classname);
1764 if (strcmp("", nc.methods[i].methodCalls[j].methodname) != 0) {
1765 ncc->methods[i].methodCalls[j].methodname = utf_new_char(nc.methods[i].methodCalls[j].methodname);
1766 ncc->methods[i].methodCalls[j].descriptor = utf_new_char(nc.methods[i].methodCalls[j].descriptor);
1769 ncc->methods[i].methodCalls[j].methodname = NULL;
1770 ncc->methods[i].methodCalls[j].descriptor = NULL;
1778 /*--------------------------------------------------------*/
1780 bool natcall2utf(bool natcallcompdone) {
1783 if (natcallcompdone)
1786 for (i=0;i<NATIVECALLSSIZE; i++) {
1787 utfNativeCall (nativeCalls[i], &nativeCompCalls[i]);
1793 /*--------------------------------------------------------*/
1796 java_objectarray *builtin_asm_createclasscontextarray(classinfo **end,classinfo **start) {
1797 #warning platform dependend
1798 java_objectarray *tmpArray;
1800 classinfo **current;
1802 size_t size=(((size_t)start)-((size_t)end)) / sizeof (classinfo*);
1803 /* printf("end %p, start %p, size %ld\n",end,start,size);*/
1804 if (!class_java_lang_Class)
1805 class_java_lang_Class = class_new(utf_new_char ("java/lang/Class"));
1806 if (!class_java_lang_SecurityManager)
1807 class_java_lang_SecurityManager = class_new(utf_new_char ("java/lang/SecurityManager"));
1809 if (start==class_java_lang_SecurityManager) {
1814 tmpArray=builtin_newarray(size, class_array_of(class_java_lang_Class)->vftbl);
1816 for(i=0,current=start;i<size;i++,current--) {
1818 /* printf("%d\n",i);
1819 utf_display(c->name);*/
1820 use_class_as_object(c);
1821 tmpArray->data[i]=c;
1827 java_lang_ClassLoader *builtin_asm_getclassloader(classinfo **end,classinfo **start) {
1828 #warning platform dependend
1830 classinfo **current;
1832 classinfo *privilegedAction;
1833 size_t size=(((size_t)start)-((size_t)end)) / sizeof (classinfo*);
1834 /* log_text("builtin_asm_getclassloader");
1835 printf("end %p, start %p, size %ld\n",end,start,size);*/
1837 if (!class_java_lang_SecurityManager)
1838 class_java_lang_SecurityManager = class_new(utf_new_char ("java/lang/SecurityManager"));
1840 if (start==class_java_lang_SecurityManager) {
1846 privilegedAction=class_new(utf_new_char("java/security/PrivilegedAction"));
1848 for(i=0,current=start;i<size;i++,current--) {
1850 if (c==privilegedAction) return NULL;
1851 if (c->classloader) return c->classloader;
1859 log_text("Java_java_lang_VMSecurityManager_currentClassLoader");
1860 init_systemclassloader();
1862 return SystemClassLoader;*/
1866 * These are local overrides for various environment variables in Emacs.
1867 * Please do not remove this and leave it at the end of the file, where
1868 * Emacs will automagically detect them.
1869 * ---------------------------------------------------------------------
1872 * indent-tabs-mode: t