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 1238 2004-06-30 20:05:30Z twisti $
59 #include "toolbox/logging.h"
60 #include "toolbox/memory.h"
61 #include "threads/thread.h"
62 #include "threads/threadio.h"
63 #include "threads/locks.h"
64 #include "nat/java_lang_VMClass.h"
65 #include "nat/java_lang_Throwable.h"
67 /* Include files for IO functions */
71 #include <sys/types.h>
77 #include "threads/threadio.h"
79 /* include table of native functions */
81 #include "nativetable.inc"
84 /* searchpath for classfiles */
87 /* for java-string to char conversion */
88 #define MAXSTRINGSIZE 1000
91 /******************** systemclasses required for native methods ***************/
93 classinfo *class_java_lang_Class;
94 classinfo *class_java_lang_VMClass;
95 classinfo *class_java_lang_System;
96 classinfo *class_java_lang_ClassLoader;
97 classinfo *class_gnu_java_lang_SystemClassLoader;
98 classinfo *class_java_lang_SecurityManager;
99 classinfo *class_java_lang_Double;
100 classinfo *class_java_lang_Float;
101 classinfo *class_java_lang_Long;
102 classinfo *class_java_lang_Byte;
103 classinfo *class_java_lang_Short;
104 classinfo *class_java_lang_Boolean;
105 classinfo *class_java_lang_Void;
106 classinfo *class_java_lang_Character;
107 classinfo *class_java_lang_Integer;
109 methodinfo *method_vmclass_init;
112 /* system exception classes required in cacao */
114 classinfo *class_java_lang_Throwable;
115 classinfo *class_java_lang_Exception;
116 classinfo *class_java_lang_Error;
119 /* exception/error super class */
121 char *string_java_lang_Throwable =
122 "java/lang/Throwable";
124 char *string_java_lang_VMThrowable =
125 "java/lang/VMThrowable";
128 /* specify some exception strings for code generation */
130 char *string_java_lang_ArithmeticException =
131 "java/lang/ArithmeticException";
133 char *string_java_lang_ArithmeticException_message =
136 char *string_java_lang_ArrayIndexOutOfBoundsException =
137 "java/lang/ArrayIndexOutOfBoundsException";
139 char *string_java_lang_ArrayStoreException =
140 "java/lang/ArrayStoreException";
142 char *string_java_lang_ClassCastException =
143 "java/lang/ClassCastException";
145 char *string_java_lang_ClassNotFoundException =
146 "java/lang/ClassNotFoundException";
148 char *string_java_lang_CloneNotSupportedException =
149 "java/lang/CloneNotSupportedException";
151 char *string_java_lang_Exception =
152 "java/lang/Exception";
154 char *string_java_lang_IllegalArgumentException =
155 "java/lang/IllegalArgumentException";
157 char *string_java_lang_IllegalMonitorStateException =
158 "java/lang/IllegalMonitorStateException";
160 char *string_java_lang_NegativeArraySizeException =
161 "java/lang/NegativeArraySizeException";
163 char *string_java_lang_NoSuchFieldException =
164 "java/lang/NoSuchFieldException";
166 char *string_java_lang_NoSuchMethodException =
167 "java/lang/NoSuchMethodException";
169 char *string_java_lang_NullPointerException =
170 "java/lang/NullPointerException";
173 /* specify some error strings for code generation */
175 char *string_java_lang_AbstractMethodError =
176 "java/lang/AbstractMethodError";
178 char *string_java_lang_ClassCircularityError =
179 "java/lang/ClassCircularityError";
181 char *string_java_lang_ClassFormatError =
182 "java/lang/ClassFormatError";
184 char *string_java_lang_Error =
187 char *string_java_lang_ExceptionInInitializerError =
188 "java/lang/ExceptionInInitializerError";
190 char *string_java_lang_IncompatibleClassChangeError =
191 "java/lang/IncompatibleClassChangeError";
193 char *string_java_lang_InternalError =
194 "java/lang/InternalError";
196 char *string_java_lang_LinkageError =
197 "java/lang/LinkageError";
199 char *string_java_lang_NoClassDefFoundError =
200 "java/lang/NoClassDefFoundError";
202 char *string_java_lang_NoSuchFieldError =
203 "java/lang/NoSuchFieldError";
205 char *string_java_lang_NoSuchMethodError =
206 "java/lang/NoSuchMethodError";
208 char *string_java_lang_OutOfMemoryError =
209 "java/lang/OutOfMemoryError";
211 char *string_java_lang_VerifyError =
212 "java/lang/VerifyError";
214 char *string_java_lang_VirtualMachineError =
215 "java/lang/VirtualMachineError";
218 /* the system classloader object */
219 struct java_lang_ClassLoader *SystemClassLoader = NULL;
221 /* for raising exceptions from native methods */
222 #if !defined(USE_THREADS) || !defined(NATIVE_THREADS)
223 java_objectheader* _exceptionptr = NULL;
226 /************* use classinfo structure as java.lang.Class object **************/
228 void use_class_as_object(classinfo *c)
230 if (!c->classvftbl) {
231 c->classvftbl = true;
233 c->header.vftbl = class_java_lang_Class->vftbl;
239 /*************************** include native methods ***************************/
242 #include "nat/GdkGraphics.c"
243 #include "nat/GtkComponentPeer.c"
244 #include "nat/GdkPixbufDecoder.c"
245 #include "nat/GtkScrollPanePeer.c"
246 #include "nat/GtkFileDialogPeer.c"
247 #include "nat/GtkLabelPeer.c"
251 /************************** tables for methods ********************************/
256 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
258 /* table for fast string comparison */
259 static nativecompref nativecomptable[NATIVETABLESIZE];
261 /* string comparsion table initialized */
262 static bool nativecompdone = false;
265 /******************************************************************************/
266 /******************************************************************************/
267 #include "natcalls.h"
269 /* string call comparison table initialized */
271 /******************************************************************************/
272 /******************************************************************************/
274 /* init_system_exceptions *****************************************************
276 load, link and compile exceptions used in the system
278 *******************************************************************************/
280 void init_system_exceptions()
282 /* java/lang/Throwable */
284 class_java_lang_Throwable =
285 class_new(utf_new_char(string_java_lang_Throwable));
286 class_load(class_java_lang_Throwable);
287 class_link(class_java_lang_Throwable);
289 /* java/lang/Exception */
291 class_java_lang_Exception =
292 class_new(utf_new_char(string_java_lang_Exception));
293 class_load(class_java_lang_Exception);
294 class_link(class_java_lang_Exception);
296 /* java/lang/Error */
298 class_java_lang_Error =
299 class_new(utf_new_char(string_java_lang_Error));
300 class_load(class_java_lang_Error);
301 class_link(class_java_lang_Error);
305 void compile_all_class_methods(classinfo *c)
309 for (i = 0; i < c->methodscount; i++) {
310 (void) jit_compile(&(c->methods[i]));
315 static void throw_exception_exit_intern(bool doexit)
317 java_objectheader *xptr;
322 xptr = *exceptionptr;
324 /* clear exception, because we are calling jit code again */
325 *exceptionptr = NULL;
327 c = xptr->vftbl->class;
329 pss = class_resolveclassmethod(c,
330 utf_new_char("printStackTrace"),
332 class_java_lang_Object,
335 /* print the stacktrace */
337 asm_calljavafunction(pss, xptr, NULL, NULL, NULL);
339 /* this normally means, we are EXTREMLY out of memory, but may be
340 any other exception */
342 utf_fprint_classname(stderr, c->name);
343 fprintf(stderr, "\n");
347 utf_fprint_classname(stderr, c->name);
348 fprintf(stderr, ": printStackTrace()V not found!\n");
361 void throw_exception()
363 throw_exception_exit_intern(false);
367 void throw_exception_exit()
369 throw_exception_exit_intern(true);
373 void throw_main_exception()
375 fprintf(stderr, "Exception in thread \"main\" ");
378 throw_exception_exit_intern(false);
382 void throw_main_exception_exit()
384 fprintf(stderr, "Exception in thread \"main\" ");
387 throw_exception_exit_intern(true);
391 void throw_cacao_exception_exit(char *exception, char *message)
397 len = strlen(exception);
398 tmp = MNEW(char, len);
399 strncpy(tmp, exception, len);
401 /* convert to classname */
403 for (i = len - 1; i >= 0; i--) {
404 if (tmp[i] == '/') tmp[i] = '.';
407 fprintf(stderr, "Exception in thread \"main\" %s", tmp);
409 MFREE(tmp, char, len);
411 if (strlen(message) > 0)
412 fprintf(stderr, ": %s", message);
414 fprintf(stderr, "\n");
422 #define CREATENEW_EXCEPTION(ex) \
423 java_objectheader *newEx; \
424 java_objectheader *oldexception=*exceptionptr;\
427 *exceptionptr=oldexception;\
430 java_objectheader *new_exception(char *classname)
432 classinfo *c = class_new(utf_new_char(classname));
434 CREATENEW_EXCEPTION(native_new_and_init(c));
437 java_objectheader *new_exception_message(char *classname, char *message)
439 classinfo *c = class_new(utf_new_char(classname));
441 CREATENEW_EXCEPTION(native_new_and_init_string(c, javastring_new_char(message)));
445 java_objectheader *new_exception_throwable(char *classname, java_lang_Throwable *throwable)
447 classinfo *c = class_new(utf_new_char(classname));
449 CREATENEW_EXCEPTION(native_new_and_init_throwable(c, throwable));
453 java_objectheader *new_exception_utfmessage(char *classname, utf *message)
455 classinfo *c = class_new(utf_new_char(classname));
457 CREATENEW_EXCEPTION(native_new_and_init_string(c, javastring_new(message)));
461 java_objectheader *new_exception_javastring(char *classname, java_lang_String *message)
463 classinfo *c = class_new(utf_new_char(classname));
465 CREATENEW_EXCEPTION(native_new_and_init_string(c, message));
469 java_objectheader *new_exception_int(char *classname, s4 i)
471 classinfo *c = class_new(utf_new_char(classname));
473 CREATENEW_EXCEPTION(native_new_and_init_int(c, i));
477 /*********************** function: native_loadclasses **************************
479 load classes required for native methods
481 *******************************************************************************/
483 void native_loadclasses()
485 static int classesLoaded = 0; /*temporary hack JoWenn*/
492 class_java_lang_Cloneable =
493 class_new(utf_new_char("java/lang/Cloneable"));
494 class_load(class_java_lang_Cloneable);
495 class_link(class_java_lang_Cloneable);
497 class_java_lang_Class =
498 class_new(utf_new_char("java/lang/Class"));
499 class_load(class_java_lang_Class);
500 class_link(class_java_lang_Class);
502 class_java_lang_VMClass =
503 class_new(utf_new_char("java/lang/VMClass"));
504 class_load(class_java_lang_VMClass);
505 class_link(class_java_lang_VMClass);
507 class_java_lang_ClassLoader =
508 class_new(utf_new_char("java/lang/ClassLoader"));
509 class_load(class_java_lang_ClassLoader);
510 class_link(class_java_lang_ClassLoader);
512 /* load classes for wrapping primitive types */
513 class_java_lang_Double = class_new(utf_new_char("java/lang/Double"));
514 class_load(class_java_lang_Double);
515 class_link(class_java_lang_Double);
517 class_java_lang_Float = class_new(utf_new_char("java/lang/Float"));
518 class_load(class_java_lang_Float);
519 class_link(class_java_lang_Float);
521 class_java_lang_Character = class_new(utf_new_char("java/lang/Character"));
522 class_load(class_java_lang_Character);
523 class_link(class_java_lang_Character);
525 class_java_lang_Integer = class_new(utf_new_char("java/lang/Integer"));
526 class_load(class_java_lang_Integer);
527 class_link(class_java_lang_Integer);
529 class_java_lang_Long = class_new(utf_new_char("java/lang/Long"));
530 class_load(class_java_lang_Long);
531 class_link(class_java_lang_Long);
533 class_java_lang_Byte = class_new(utf_new_char("java/lang/Byte"));
534 class_load(class_java_lang_Byte);
535 class_link(class_java_lang_Byte);
537 class_java_lang_Short = class_new(utf_new_char("java/lang/Short"));
538 class_load(class_java_lang_Short);
539 class_link(class_java_lang_Short);
541 class_java_lang_Boolean = class_new(utf_new_char("java/lang/Boolean"));
542 class_load(class_java_lang_Boolean);
543 class_link(class_java_lang_Boolean);
545 class_java_lang_Void = class_new(utf_new_char("java/lang/Void"));
546 class_load(class_java_lang_Void);
547 class_link(class_java_lang_Void);
552 /*****************************************************************************
554 create systemclassloader object and initialize instance fields
556 ******************************************************************************/
558 void init_systemclassloader()
560 log_text("init_systemclassloader");
561 if (!SystemClassLoader) {
562 native_loadclasses();
563 log_text("Initializing new system class loader");
564 /* create object and call initializer */
565 SystemClassLoader = (java_lang_ClassLoader *) native_new_and_init(class_new(utf_new_char("gnu/java/lang/SystemClassLoader")));
567 /* systemclassloader has no parent */
568 SystemClassLoader->parent = NULL;
569 SystemClassLoader->initialized = true;
571 log_text("leaving system class loader");
575 /********************* function: native_setclasspath **************************/
577 void native_setclasspath(char *path)
579 /* set searchpath for classfiles */
584 /*********************** Function: native_findfunction *************************
586 Looks up a method (must have the same class name, method name, descriptor
587 and 'static'ness) and returns a function pointer to it.
588 Returns: function pointer or NULL (if there is no such method)
590 Remark: For faster operation, the names/descriptors are converted from C
591 strings to Unicode the first time this function is called.
593 *******************************************************************************/
595 functionptr native_findfunction(utf *cname, utf *mname,
596 utf *desc, bool isstatic)
599 /* entry of table for fast string comparison */
600 struct nativecompref *n;
601 /* for warning message if no function is found */
605 isstatic = isstatic ? true : false;
607 if (!nativecompdone) {
608 for (i = 0; i < NATIVETABLESIZE; i++) {
609 nativecomptable[i].classname =
610 utf_new_char(nativetable[i].classname);
611 nativecomptable[i].methodname =
612 utf_new_char(nativetable[i].methodname);
613 nativecomptable[i].descriptor =
614 utf_new_char(nativetable[i].descriptor);
615 nativecomptable[i].isstatic =
616 nativetable[i].isstatic;
617 nativecomptable[i].func =
620 nativecompdone = true;
625 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
627 buffer = MNEW(char, buffer_len);
629 strcpy(buffer, "searching matching function in native table:");
630 utf_sprint(buffer+strlen(buffer), mname);
631 strcpy(buffer+strlen(buffer), ": ");
632 utf_sprint(buffer+strlen(buffer), desc);
633 strcpy(buffer+strlen(buffer), " for class ");
634 utf_sprint(buffer+strlen(buffer), cname);
638 MFREE(buffer, char, buffer_len);
641 for (i = 0; i < NATIVETABLESIZE; i++) {
642 n = &(nativecomptable[i]);
644 if (cname == n->classname && mname == n->methodname &&
645 desc == n->descriptor && isstatic == n->isstatic)
649 if (cname == n->classname && mname == n->methodname ) log_text("static and descriptor mismatch");
653 utf_strlen(n->classname) + utf_strlen(n->methodname) + utf_strlen(n->descriptor) + 64;
655 buffer = MNEW(char, buffer_len);
657 strcpy(buffer, "comparing with:");
658 utf_sprint(buffer+strlen(buffer), n->methodname);
659 strcpy (buffer+strlen(buffer), ": ");
660 utf_sprint(buffer+strlen(buffer), n->descriptor);
661 strcpy(buffer+strlen(buffer), " for class ");
662 utf_sprint(buffer+strlen(buffer), n->classname);
666 MFREE(buffer, char, buffer_len);
673 /* no function was found, display warning */
676 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
678 buffer = MNEW(char, buffer_len);
680 strcpy(buffer, "warning: native function ");
681 utf_sprint(buffer + strlen(buffer), mname);
682 strcpy(buffer + strlen(buffer), ": ");
683 utf_sprint(buffer + strlen(buffer), desc);
684 strcpy(buffer + strlen(buffer), " not found in class ");
685 utf_sprint(buffer + strlen(buffer), cname);
689 MFREE(buffer, char, buffer_len);
693 /* keep compiler happy */
698 /********************** function: javastring_new *******************************
700 creates a new object of type java/lang/String with the text of
701 the specified utf8-string
703 return: pointer to the string or NULL if memory is exhausted.
705 *******************************************************************************/
707 java_lang_String *javastring_new(utf *u)
709 char *utf_ptr = u->text; /* current utf character in utf string */
710 int utflength = utf_strlen(u); /* length of utf-string if uncompressed */
711 java_lang_String *s; /* result-string */
715 s = (java_lang_String *) builtin_new(class_java_lang_String);
716 a = builtin_newarray_char(utflength);
718 /* javastring or character-array could not be created */
722 /* decompress utf-string */
723 for (i = 0; i < utflength; i++)
724 a->data[i] = utf_nextu2(&utf_ptr);
726 /* set fields of the javastring-object */
729 s->count = utflength;
735 /********************** function: javastring_new_char **************************
737 creates a new java/lang/String object which contains the convertet
738 C-string passed via text.
740 return: the object pointer or NULL if memory is exhausted.
742 *******************************************************************************/
744 java_lang_String *javastring_new_char(char *text)
747 s4 len = strlen(text); /* length of the string */
748 java_lang_String *s; /* result-string */
751 s = (java_lang_String *) builtin_new(class_java_lang_String);
752 a = builtin_newarray_char(len);
754 /* javastring or character-array could not be created */
759 for (i = 0; i < len; i++)
760 a->data[i] = text[i];
762 /* set fields of the javastring-object */
771 /************************* function javastring_tochar **************************
773 converts a Java string into a C string.
775 return: pointer to C string
777 Caution: every call of this function overwrites the previous string !!!
779 *******************************************************************************/
781 static char stringbuffer[MAXSTRINGSIZE];
783 char *javastring_tochar(java_objectheader *so)
785 java_lang_String *s = (java_lang_String *) so;
797 if (s->count > MAXSTRINGSIZE)
800 for (i = 0; i < s->count; i++)
801 stringbuffer[i] = a->data[s->offset + i];
803 stringbuffer[i] = '\0';
809 /****************** function class_findfield_approx ****************************
811 searches in 'classinfo'-structure for a field with the
814 *******************************************************************************/
816 fieldinfo *class_findfield_approx(classinfo *c, utf *name)
820 for (i = 0; i < c->fieldscount; i++) {
821 /* compare field names */
822 if ((c->fields[i].name == name))
823 return &(c->fields[i]);
826 /* field was not found, raise exception */
827 *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
833 s4 class_findfield_index_approx(classinfo *c, utf *name)
837 for (i = 0; i < c->fieldscount; i++) {
838 /* compare field names */
839 if ((c->fields[i].name == name))
843 /* field was not found, raise exception */
844 *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
850 /********************** function: native_new_and_init *************************
852 Creates a new object on the heap and calls the initializer.
853 Returns the object pointer or NULL if memory is exhausted.
855 *******************************************************************************/
857 java_objectheader *native_new_and_init(classinfo *c)
860 java_objectheader *o;
863 return *exceptionptr;
865 o = builtin_new(c); /* create object */
870 /* find initializer */
872 m = class_findmethod(c,
873 utf_new_char("<init>"),
874 utf_new_char("()V"));
876 if (!m) { /* initializer not found */
878 char logtext[MAXLOGTEXT];
879 sprintf(logtext, "Warning: class has no instance-initializer: ");
880 utf_sprint_classname(logtext + strlen(logtext), c->name);
886 /* call initializer */
888 asm_calljavafunction(m, o, NULL, NULL, NULL);
894 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
897 java_objectheader *o;
900 return *exceptionptr;
902 o = builtin_new(c); /* create object */
907 /* find initializer */
909 m = class_findmethod(c,
910 utf_new_char("<init>"),
911 utf_new_char("(Ljava/lang/String;)V"));
913 if (!m) { /* initializer not found */
915 char logtext[MAXLOGTEXT];
916 sprintf(logtext, "Warning: class has no instance-initializer: ");
917 utf_sprint_classname(logtext + strlen(logtext), c->name);
923 /* call initializer */
925 asm_calljavafunction(m, o, s, NULL, NULL);
931 java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
934 java_objectheader *o;
937 return *exceptionptr;
939 o = builtin_new(c); /* create object */
943 /* find initializer */
945 m = class_findmethod(c,
946 utf_new_char("<init>"),
947 utf_new_char("(I)V"));
949 if (!m) { /* initializer not found */
951 char logtext[MAXLOGTEXT];
952 sprintf(logtext, "Warning: class has no instance-initializer: ");
953 utf_sprint_classname(logtext + strlen(logtext), c->name);
959 /* call initializer */
961 #if defined(__I386__) || defined(__POWERPC__)
962 asm_calljavafunction(m, o, (void *) i, NULL, NULL);
964 asm_calljavafunction(m, o, (void *) (s8) i, NULL, NULL);
971 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t)
974 java_objectheader *o;
977 return *exceptionptr;
979 o = builtin_new(c); /* create object */
983 /* find initializer */
985 m = class_findmethod(c,
986 utf_new_char("<init>"),
987 utf_new_char("(Ljava/lang/Throwable;)V"));
989 if (!m) { /* initializer not found */
991 char logtext[MAXLOGTEXT];
992 sprintf(logtext, "Warning: class has no instance-initializer: ");
993 utf_sprint_classname(logtext + strlen(logtext), c->name);
999 /* call initializer */
1001 asm_calljavafunction(m, o, t, NULL, NULL);
1007 /******************** function: stringtable_update ****************************
1009 traverses the javastring hashtable and sets the vftbl-entries of
1010 javastrings which were temporarily set to NULL, because
1011 java.lang.Object was not yet loaded
1013 *******************************************************************************/
1015 void stringtable_update ()
1017 java_lang_String *js;
1019 literalstring *s; /* hashtable entry */
1022 for (i = 0; i < string_hash.size; i++) {
1023 s = string_hash.ptr[i];
1027 js = (java_lang_String *) s->string;
1029 if (!js || !js->value)
1030 /* error in hashtable found */
1031 panic("invalid literalstring in hashtable");
1035 if (!js->header.vftbl)
1036 /* vftbl of javastring is NULL */
1037 js->header.vftbl = class_java_lang_String->vftbl;
1039 if (!a->header.objheader.vftbl)
1040 /* vftbl of character-array is NULL */
1041 a->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
1043 /* follow link in external hash chain */
1051 /************************* function: u2_utflength ***************************
1053 returns the utf length in bytes of a u2 array
1055 *****************************************************************************/
1057 u4 u2_utflength(u2 *text, u4 u2_length)
1059 u4 result_len = 0; /* utf length in bytes */
1060 u2 ch; /* current unicode character */
1063 for (len = 0; len < u2_length; len++) {
1064 /* next unicode character */
1067 /* determine bytes required to store unicode character as utf */
1068 if (ch && (ch < 0x80))
1070 else if (ch < 0x800)
1080 /********************* function: utf_new_u2 ***********************************
1082 make utf symbol from u2 array,
1083 if isclassname is true '.' is replaced by '/'
1085 *******************************************************************************/
1087 utf *utf_new_u2(u2 *unicode_pos, u4 unicode_length, bool isclassname)
1089 char *buffer; /* memory buffer for unicode characters */
1090 char *pos; /* pointer to current position in buffer */
1091 u4 left; /* unicode characters left */
1092 u4 buflength; /* utf length in bytes of the u2 array */
1093 utf *result; /* resulting utf-string */
1096 /* determine utf length in bytes and allocate memory */
1097 /* printf("utf_new_u2: unicode_length=%d\n",unicode_length); */
1098 buflength = u2_utflength(unicode_pos, unicode_length);
1099 buffer = MNEW(char, buflength);
1104 for (i = 0; i++ < unicode_length; unicode_pos++) {
1105 /* next unicode character */
1106 u2 c = *unicode_pos;
1108 if ((c != 0) && (c < 0x80)) {
1111 if ((int) left < 0) break;
1112 /* convert classname */
1113 if (isclassname && c == '.')
1118 } else if (c < 0x800) {
1120 unsigned char high = c >> 6;
1121 unsigned char low = c & 0x3F;
1123 if ((int) left < 0) break;
1124 *pos++ = high | 0xC0;
1125 *pos++ = low | 0x80;
1129 char low = c & 0x3f;
1130 char mid = (c >> 6) & 0x3F;
1131 char high = c >> 12;
1133 if ((int) left < 0) break;
1134 *pos++ = high | 0xE0;
1135 *pos++ = mid | 0x80;
1136 *pos++ = low | 0x80;
1140 /* insert utf-string into symbol-table */
1141 result = utf_new(buffer,buflength);
1143 MFREE(buffer, char, buflength);
1149 /********************* function: javastring_toutf *****************************
1151 make utf symbol from javastring
1153 *******************************************************************************/
1155 utf *javastring_toutf(java_lang_String *string, bool isclassname)
1157 java_lang_String *str = (java_lang_String *) string;
1159 /* printf("javastring_toutf offset: %d, len %d\n",str->offset, str->count); */
1160 /* fflush(stdout); */
1162 return utf_new_u2(str->value->data + str->offset, str->count, isclassname);
1166 /********************* function: literalstring_u2 *****************************
1168 searches for the javastring with the specified u2-array in
1169 the string hashtable, if there is no such string a new one is
1172 if copymode is true a copy of the u2-array is made
1174 *******************************************************************************/
1176 java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset,
1179 literalstring *s; /* hashtable element */
1180 java_lang_String *js; /* u2-array wrapped in javastring */
1181 java_chararray *stringdata; /* copy of u2-array */
1187 //#define DEBUG_LITERALSTRING_U2
1188 #ifdef DEBUG_LITERALSTRING_U2
1189 printf("literalstring_u2: length=%d, offset=%d\n", length, offset);
1193 /* find location in hashtable */
1194 key = unicode_hashkey(a->data + offset, length);
1195 slot = key & (string_hash.size - 1);
1196 s = string_hash.ptr[slot];
1199 js = (java_lang_String *) s->string;
1201 if (length == js->count) {
1203 for (i = 0; i < length; i++) {
1204 if (a->data[offset + i] != js->value->data[i])
1208 /* string already in hashtable, free memory */
1210 lit_mem_free(a, sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10);
1212 #ifdef DEBUG_LITERALSTRING_U2
1213 printf("literalstring_u2: foundentry at %p\n", js);
1214 utf_display(javastring_toutf(js, 0));
1218 return (java_objectheader *) js;
1222 /* follow link in external hash chain */
1227 /* create copy of u2-array for new javastring */
1228 u4 arraysize = sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10;
1229 stringdata = lit_mem_alloc(arraysize);
1230 /* memcpy(stringdata, a, arraysize); */
1231 memcpy(&(stringdata->header), &(a->header), sizeof(java_arrayheader));
1232 memcpy(&(stringdata->data), &(a->data) + offset, sizeof(u2) * (length - 1) + 10);
1238 /* location in hashtable found, complete arrayheader */
1239 stringdata->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
1240 stringdata->header.size = length;
1242 /* if we use eager loading, we have to check loaded String class */
1244 class_java_lang_String =
1245 class_new_intern(utf_new_char("java/lang/String"));
1247 if (!class_load(class_java_lang_String))
1250 list_addfirst(&unlinkedclasses, class_java_lang_String);
1253 /* create new javastring */
1254 js = LNEW(java_lang_String);
1255 js->header.vftbl = class_java_lang_String->vftbl;
1256 js->value = stringdata;
1260 #ifdef DEBUG_LITERALSTRING_U2
1261 printf("literalstring_u2: newly created at %p\n", js);
1262 utf_display(javastring_toutf(js, 0));
1267 /* create new literalstring */
1268 s = NEW(literalstring);
1269 s->hashlink = string_hash.ptr[slot];
1270 s->string = (java_objectheader *) js;
1271 string_hash.ptr[slot] = s;
1273 /* update number of hashtable entries */
1274 string_hash.entries++;
1276 /* reorganization of hashtable */
1277 if (string_hash.entries > (string_hash.size * 2)) {
1278 /* reorganization of hashtable, average length of
1279 the external chains is approx. 2 */
1283 hashtable newhash; /* the new hashtable */
1285 /* create new hashtable, double the size */
1286 init_hashtable(&newhash, string_hash.size * 2);
1287 newhash.entries = string_hash.entries;
1289 /* transfer elements to new hashtable */
1290 for (i = 0; i < string_hash.size; i++) {
1291 s = string_hash.ptr[i];
1293 literalstring *nexts = s->hashlink;
1294 js = (java_lang_String *) s->string;
1295 slot = unicode_hashkey(js->value->data, js->count) & (newhash.size - 1);
1297 s->hashlink = newhash.ptr[slot];
1298 newhash.ptr[slot] = s;
1300 /* follow link in external hash chain */
1305 /* dispose old table */
1306 MFREE(string_hash.ptr, void*, string_hash.size);
1307 string_hash = newhash;
1310 return (java_objectheader *) js;
1314 /******************** Function: literalstring_new *****************************
1316 creates a new javastring with the text of the utf-symbol
1317 and inserts it into the string hashtable
1319 *******************************************************************************/
1321 java_objectheader *literalstring_new(utf *u)
1323 char *utf_ptr = u->text; /* pointer to current unicode character in utf string */
1324 u4 utflength = utf_strlen(u); /* length of utf-string if uncompressed */
1325 java_chararray *a; /* u2-array constructed from utf string */
1328 /* allocate memory */
1329 a = lit_mem_alloc(sizeof(java_chararray) + sizeof(u2) * (utflength - 1) + 10);
1331 /* convert utf-string to u2-array */
1332 for (i = 0; i < utflength; i++)
1333 a->data[i] = utf_nextu2(&utf_ptr);
1335 return literalstring_u2(a, utflength, 0, false);
1339 /********************** function: literalstring_free **************************
1341 removes a javastring from memory
1343 ******************************************************************************/
1345 void literalstring_free(java_objectheader* sobj)
1347 java_lang_String *s = (java_lang_String *) sobj;
1348 java_chararray *a = s->value;
1350 /* dispose memory of java.lang.String object */
1351 LFREE(s, java_lang_String);
1353 /* dispose memory of java-characterarray */
1354 LFREE(a, sizeof(java_chararray) + sizeof(u2) * (a->header.size - 1)); /* +10 ?? */
1358 void copy_vftbl(vftbl **dest, vftbl *src)
1362 /* XXX this kind of copying does not work (in the general
1363 * case). The interface tables would have to be copied, too. I
1364 * don't see why we should make a copy anyway. -Edwin
1366 *dest = mem_alloc(sizeof(vftbl) + sizeof(methodptr)*(src->vftbllength-1));
1367 memcpy(*dest, src, sizeof(vftbl) - sizeof(methodptr));
1368 memcpy(&(*dest)->table, &src->table, src->vftbllength * sizeof(methodptr));
1373 /******************************************************************************************
1375 creates method signature (excluding return type) from array of
1376 class-objects representing the parameters of the method
1378 *******************************************************************************************/
1381 utf *create_methodsig(java_objectarray* types, char *retType)
1383 char *buffer; /* buffer for building the desciptor */
1384 char *pos; /* current position in buffer */
1385 utf *result; /* the method signature */
1386 u4 buffer_size = 3; /* minimal size=3: room for parenthesis and returntype */
1389 if (!types) return NULL;
1391 /* determine required buffer-size */
1392 for (i = 0; i < types->header.size; i++) {
1393 classinfo *c = (classinfo *) types->data[i];
1394 buffer_size = buffer_size + c->name->blength + 2;
1397 if (retType) buffer_size += strlen(retType);
1399 /* allocate buffer */
1400 buffer = MNEW(u1, buffer_size);
1403 /* method-desciptor starts with parenthesis */
1406 for (i = 0; i < types->header.size; i++) {
1409 /* current argument */
1410 classinfo *c = (classinfo *) types->data[i];
1412 /* current position in utf-text */
1413 char *utf_ptr = c->name->text;
1415 /* determine type of argument */
1416 if ((ch = utf_nextu2(&utf_ptr)) == '[') {
1418 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1419 *pos++ = *utf_ptr; /* copy text */
1423 /* check for primitive types */
1424 for (j = 0; j < PRIMITIVETYPE_COUNT; j++) {
1425 char *utf_pos = utf_ptr - 1;
1426 char *primitive = primitivetype_table[j].wrapname;
1429 while (utf_pos < utf_end(c->name)) {
1430 if (*utf_pos++ != *primitive++) goto nomatch;
1433 /* primitive type found */
1434 *pos++ = primitivetype_table[j].typesig;
1441 /* no primitive type and no arrayclass, so must be object */
1445 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1459 for (i = 0; i < strlen(retType); i++) {
1460 *pos++ = retType[i];
1464 /* create utf-string */
1465 result = utf_new(buffer, (pos - buffer));
1466 MFREE(buffer, u1, buffer_size);
1472 /******************************************************************************************
1474 retrieve the next argument or returntype from a descriptor
1475 and return the corresponding class
1477 *******************************************************************************************/
1479 classinfo *get_type(char **utf_ptr,char *desc_end, bool skip)
1481 classinfo *c = class_from_descriptor(*utf_ptr,desc_end,utf_ptr,
1482 (skip) ? CLASSLOAD_SKIP : CLASSLOAD_LOAD);
1485 panic("illegal descriptor");
1487 if (skip) return NULL;
1489 use_class_as_object(c);
1494 /* get_parametertypes **********************************************************
1496 use the descriptor of a method to generate a java/lang/Class array
1497 which contains the classes of the parametertypes of the method
1499 *******************************************************************************/
1501 java_objectarray* get_parametertypes(methodinfo *m)
1503 utf *descr = m->descriptor; /* method-descriptor */
1504 char *utf_ptr = descr->text; /* current position in utf-text */
1505 char *desc_end = utf_end(descr); /* points behind utf string */
1506 java_objectarray* result;
1507 int parametercount = 0;
1511 utf_nextu2(&utf_ptr);
1513 /* determine number of parameters */
1514 while (*utf_ptr != ')') {
1515 get_type(&utf_ptr, desc_end, true);
1519 /* create class-array */
1520 result = builtin_anewarray(parametercount, class_java_lang_Class);
1522 utf_ptr = descr->text;
1523 utf_nextu2(&utf_ptr);
1525 /* get returntype classes */
1526 for (i = 0; i < parametercount; i++)
1528 (java_objectheader *) get_type(&utf_ptr, desc_end, false);
1534 /* get_exceptiontypes **********************************************************
1536 get the exceptions which can be thrown by a method
1538 *******************************************************************************/
1540 java_objectarray* get_exceptiontypes(methodinfo *m) {
1541 u2 exccount=m->thrownexceptionscount;
1543 java_objectarray *result;
1544 /* create class-array */
1545 result = builtin_anewarray(exccount, class_java_lang_Class);
1546 for (i=0;i<exccount;i++) {
1547 java_objectheader *oh=(java_objectheader*)(m->thrownexceptions[i]);
1548 use_class_as_object(oh);
1558 /******************************************************************************************
1560 get the returntype class of a method
1562 *******************************************************************************************/
1564 classinfo *get_returntype(methodinfo *m)
1566 char *utf_ptr; /* current position in utf-text */
1567 char *desc_end; /* points behind utf string */
1568 utf *desc = m->descriptor; /* method-descriptor */
1570 utf_ptr = desc->text;
1571 desc_end = utf_end(desc);
1573 /* ignore parametertypes */
1574 while ((utf_ptr<desc_end) && utf_nextu2(&utf_ptr)!=')')
1577 return get_type(&utf_ptr,desc_end, false);
1581 /*****************************************************************************/
1582 /*****************************************************************************/
1585 /*--------------------------------------------------------*/
1586 void printNativeCall(nativeCall nc) {
1589 printf("\n%s's Native Methods call:\n",nc.classname); fflush(stdout);
1590 for (i=0; i<nc.methCnt; i++) {
1591 printf("\tMethod=%s %s\n",nc.methods[i].methodname, nc.methods[i].descriptor);fflush(stdout);
1593 for (j=0; j<nc.callCnt[i]; j++) {
1594 printf("\t\t<%i,%i>aCalled = %s %s %s\n",i,j,
1595 nc.methods[i].methodCalls[j].classname,
1596 nc.methods[i].methodCalls[j].methodname,
1597 nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1600 printf("-+++++--------------------\n");fflush(stdout);
1603 /*--------------------------------------------------------*/
1604 void printCompNativeCall(nativeCompCall nc) {
1606 printf("printCompNativeCall BEGIN\n");fflush(stdout);
1607 printf("\n%s's Native Comp Methods call:\n",nc.classname->text);fflush(stdout);
1608 utf_display(nc.classname); fflush(stdout);
1610 for (i=0; i<nc.methCnt; i++) {
1611 printf("\tMethod=%s %s\n",nc.methods[i].methodname->text,nc.methods[i].descriptor->text);fflush(stdout);
1612 utf_display(nc.methods[i].methodname); fflush(stdout);
1613 utf_display(nc.methods[i].descriptor);fflush(stdout);
1614 printf("\n");fflush(stdout);
1616 for (j=0; j<nc.callCnt[i]; j++) {
1617 printf("\t\t<%i,%i>bCalled = ",i,j);fflush(stdout);
1618 utf_display(nc.methods[i].methodCalls[j].classname);fflush(stdout);
1619 utf_display(nc.methods[i].methodCalls[j].methodname); fflush(stdout);
1620 utf_display(nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1621 printf("\n");fflush(stdout);
1624 printf("---------------------\n");fflush(stdout);
1628 /*--------------------------------------------------------*/
1629 classMeth findNativeMethodCalls(utf *c, utf *m, utf *d )
1642 /*--------------------------------------------------------*/
1643 nativeCall* findNativeClassCalls(char *aclassname ) {
1646 for (i=0;i<NATIVECALLSSIZE; i++) {
1647 /* convert table to utf later to speed up search */
1648 if (strcmp(nativeCalls[i].classname, aclassname) == 0)
1649 return &nativeCalls[i];
1654 /*--------------------------------------------------------*/
1655 /*--------------------------------------------------------*/
1656 void utfNativeCall(nativeCall nc, nativeCompCall *ncc) {
1660 ncc->classname = utf_new_char(nc.classname);
1661 ncc->methCnt = nc.methCnt;
1663 for (i=0; i<nc.methCnt; i++) {
1664 ncc->methods[i].methodname = utf_new_char(nc.methods[i].methodname);
1665 ncc->methods[i].descriptor = utf_new_char(nc.methods[i].descriptor);
1666 ncc->callCnt[i] = nc.callCnt[i];
1668 for (j=0; j<nc.callCnt[i]; j++) {
1670 ncc->methods[i].methodCalls[j].classname = utf_new_char(nc.methods[i].methodCalls[j].classname);
1672 if (strcmp("", nc.methods[i].methodCalls[j].methodname) != 0) {
1673 ncc->methods[i].methodCalls[j].methodname = utf_new_char(nc.methods[i].methodCalls[j].methodname);
1674 ncc->methods[i].methodCalls[j].descriptor = utf_new_char(nc.methods[i].methodCalls[j].descriptor);
1677 ncc->methods[i].methodCalls[j].methodname = NULL;
1678 ncc->methods[i].methodCalls[j].descriptor = NULL;
1686 /*--------------------------------------------------------*/
1688 bool natcall2utf(bool natcallcompdone) {
1691 if (natcallcompdone)
1694 for (i=0;i<NATIVECALLSSIZE; i++) {
1695 utfNativeCall (nativeCalls[i], &nativeCompCalls[i]);
1701 /*--------------------------------------------------------*/
1704 java_objectarray *builtin_asm_createclasscontextarray(classinfo **end,classinfo **start)
1706 #if defined(__GNUC__)
1707 #warning platform dependend
1709 java_objectarray *tmpArray;
1711 classinfo **current;
1713 size_t size=(((size_t)start)-((size_t)end)) / sizeof (classinfo*);
1714 /*printf("end %p, start %p, size %ld\n",end,start,size);*/
1715 if (!class_java_lang_Class)
1716 class_java_lang_Class = class_new(utf_new_char ("java/lang/Class"));
1717 if (!class_java_lang_SecurityManager)
1718 class_java_lang_SecurityManager = class_new(utf_new_char ("java/lang/SecurityManager"));
1720 if (start==class_java_lang_SecurityManager) {
1725 tmpArray=builtin_newarray(size, class_array_of(class_java_lang_Class)->vftbl);
1727 for(i=0,current=start;i<size;i++,current--) {
1729 /* printf("%d\n",i);
1730 utf_display(c->name);*/
1731 use_class_as_object(c);
1732 tmpArray->data[i]=c;
1738 java_lang_ClassLoader *builtin_asm_getclassloader(classinfo **end,classinfo **start)
1740 #if defined(__GNUC__)
1741 #warning platform dependend
1744 classinfo **current;
1746 classinfo *privilegedAction;
1747 size_t size=(((size_t)start)-((size_t)end)) / sizeof (classinfo*);
1748 /* log_text("builtin_asm_getclassloader");
1749 printf("end %p, start %p, size %ld\n",end,start,size);*/
1751 if (!class_java_lang_SecurityManager)
1752 class_java_lang_SecurityManager = class_new(utf_new_char ("java/lang/SecurityManager"));
1754 if (start==class_java_lang_SecurityManager) {
1760 privilegedAction=class_new(utf_new_char("java/security/PrivilegedAction"));
1762 for(i=0,current=start;i<size;i++,current--) {
1764 if (c==privilegedAction) return NULL;
1765 if (c->classloader) return c->classloader;
1773 log_text("Java_java_lang_VMSecurityManager_currentClassLoader");
1774 init_systemclassloader();
1776 return SystemClassLoader;*/
1780 * These are local overrides for various environment variables in Emacs.
1781 * Please do not remove this and leave it at the end of the file, where
1782 * Emacs will automagically detect them.
1783 * ---------------------------------------------------------------------
1786 * indent-tabs-mode: t