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 1033 2004-04-26 16:18:56Z twisti $
53 #include "nativetypes.hh"
59 #include "toolbox/loging.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 /* searchpath for classfiles */
82 /* for java-string to char conversion */
83 #define MAXSTRINGSIZE 1000
85 /******************** systemclasses required for native methods ***************/
87 classinfo *class_java_lang_Class;
88 classinfo *class_java_lang_VMClass;
89 classinfo *class_java_lang_System;
90 classinfo *class_java_lang_ClassLoader;
91 classinfo *class_gnu_java_lang_SystemClassLoader;
92 classinfo *class_java_lang_SecurityManager;
93 classinfo *class_java_lang_Double;
94 classinfo *class_java_lang_Float;
95 classinfo *class_java_lang_Long;
96 classinfo *class_java_lang_Byte;
97 classinfo *class_java_lang_Short;
98 classinfo *class_java_lang_Boolean;
99 classinfo *class_java_lang_Void;
100 classinfo *class_java_lang_Character;
101 classinfo *class_java_lang_Integer;
103 methodinfo *method_vmclass_init;
106 /* specify some exception strings for code generation */
108 char *string_java_lang_ArithmeticException =
109 "java/lang/ArithmeticException";
111 char *string_java_lang_ArithmeticException_message =
114 char *string_java_lang_ArrayIndexOutOfBoundsException =
115 "java/lang/ArrayIndexOutOfBoundsException";
117 char *string_java_lang_ArrayStoreException =
118 "java/lang/ArrayStoreException";
120 char *string_java_lang_ClassCastException =
121 "java/lang/ClassCastException";
123 char *string_java_lang_ClassNotFoundException =
124 "java/lang/ClassNotFoundException";
126 char *string_java_lang_CloneNotSupportedException =
127 "java/lang/CloneNotSupportedException";
129 char *string_java_lang_IllegalArgumentException =
130 "java/lang/IllegalArgumentException";
132 char *string_java_lang_IllegalMonitorStateException =
133 "java/lang/IllegalMonitorStateException";
135 char *string_java_lang_NegativeArraySizeException =
136 "java/lang/NegativeArraySizeException";
138 char *string_java_lang_NoSuchFieldException =
139 "java/lang/NoSuchFieldException";
141 char *string_java_lang_NoSuchMethodException =
142 "java/lang/NoSuchMethodException";
144 char *string_java_lang_NullPointerException =
145 "java/lang/NullPointerException";
148 /* specify some error strings for code generation */
150 char *string_java_lang_ClassCircularityError =
151 "java/lang/ClassCircularityError";
153 char *string_java_lang_ClassFormatError =
154 "java/lang/ClassFormatError";
156 char *string_java_lang_LinkageError =
157 "java/lang/LinkageError";
159 char *string_java_lang_NoClassDefFoundError =
160 "java/lang/NoClassDefFoundError";
162 char *string_java_lang_NoSuchFieldError =
163 "java/lang/NoSuchFieldError";
165 char *string_java_lang_NoSuchMethodError =
166 "java/lang/NoSuchMethodError";
168 char *string_java_lang_OutOfMemoryError =
169 "java/lang/OutOfMemoryError";
172 /* the system classloader object */
173 struct java_lang_ClassLoader *SystemClassLoader = NULL;
175 /* for raising exceptions from native methods */
176 #if !defined(USE_THREADS) || !defined(NATIVE_THREADS)
177 java_objectheader* _exceptionptr = NULL;
180 /************* use classinfo structure as java.lang.Class object **************/
182 void use_class_as_object(classinfo *c)
186 java_objectheader *vmo;
188 /* vt = class_java_lang_Class->vftbl; */
190 if (!c->classvftbl) {
191 c->classvftbl = true;
193 /* copy_vftbl(&newtbl, vt);
194 newtbl->class = c->header.vftbl->class;
195 newtbl->baseval = c->header.vftbl->baseval;
196 newtbl->diffval = c->header.vftbl->diffval;
197 c->header.vftbl = newtbl;*/
199 c->header.vftbl = class_java_lang_Class->vftbl;
201 method_vmclass_init =
202 class_findmethod(class_java_lang_VMClass,
203 utf_new_char("<init>"),
204 utf_new_char("(Lgnu/classpath/RawData;)V"));
206 if (method_vmclass_init == 0) {
207 class_showmethods(class_java_lang_VMClass);
208 panic("Needed class initializer for VMClass could not be found");
212 vmo = builtin_new(class_java_lang_VMClass);
215 panic("Error while creating instance of java/lang/VMClass");
217 asm_calljavafunction(method_vmclass_init, vmo, c, NULL, NULL);
219 c->vmClass = (java_lang_VMClass *) vmo;
223 /*************************** include native methods ***************************/
226 #include "nat/GdkGraphics.c"
227 #include "nat/GtkComponentPeer.c"
228 #include "nat/GdkPixbufDecoder.c"
229 #include "nat/GtkScrollPanePeer.c"
230 #include "nat/GtkFileDialogPeer.c"
231 #include "nat/GtkLabelPeer.c"
235 /************************** tables for methods ********************************/
240 /* table for locating native methods */
241 static struct nativeref {
249 #include "nativetable.hh"
254 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
256 /* table for fast string comparison */
257 static struct nativecompref {
263 } nativecomptable [NATIVETABLESIZE];
265 /* string comparsion table initialized */
266 static bool nativecompdone = false;
269 /******************************************************************************/
270 /******************************************************************************/
271 #include "natcalls.h"
273 /* string call comparison table initialized */
275 /******************************************************************************/
276 /******************************************************************************/
278 /*--------------- native method calls & classes used -------------------------*/
280 void throw_exception_exit()
282 java_objectheader *xptr;
287 xptr = *exceptionptr;
289 /* clear exception, because we are calling jit code again */
290 *exceptionptr = NULL;
292 printf("Exception in thread \"main\" ");
295 c = xptr->vftbl->class;
297 /* ts = class_resolveclassmethod(c, */
298 /* utf_new_char("toString"), */
299 /* utf_new_char("()Ljava/lang/String;"), */
300 /* class_java_lang_Object, */
304 /* panic("internal error"); */
307 /* tostring = asm_calljavafunction(ts, xptr, NULL, NULL, NULL); */
309 /* utf_display(javastring_toutf(tostring, false)); */
311 pss = class_resolveclassmethod(c,
312 utf_new_char("printStackTrace"),
314 class_java_lang_Object,
317 /* print the stacktrace */
319 asm_calljavafunction(pss, xptr, NULL, NULL, NULL);
322 panic("printStackTrace not found!");
333 java_objectheader *new_exception(char *classname)
335 classinfo *c = class_new(utf_new_char(classname));
337 return native_new_and_init(c);
340 java_objectheader *new_exception_message(char *classname, char *message)
342 classinfo *c = class_new(utf_new_char(classname));
344 return native_new_and_init_string(c, javastring_new_char(message));
348 java_objectheader *new_exception_throwable(char *classname, java_lang_Throwable *throwable)
350 classinfo *c = class_new(utf_new_char(classname));
352 return native_new_and_init_throwable(c, throwable);
356 java_objectheader *new_exception_utfmessage(char *classname, utf *message)
358 classinfo *c = class_new(utf_new_char(classname));
360 return native_new_and_init_string(c, javastring_new(message));
364 java_objectheader *new_exception_javastring(char *classname, java_lang_String *message)
366 classinfo *c = class_new(utf_new_char(classname));
368 return native_new_and_init_string(c, message);
372 java_objectheader *new_exception_int(char *classname, s4 i)
374 classinfo *c = class_new(utf_new_char(classname));
376 return native_new_and_init_int(c, i);
380 /*********************** function: native_loadclasses **************************
382 load classes required for native methods
384 *******************************************************************************/
386 void native_loadclasses()
388 static int classesLoaded = 0; /*temporary hack JoWenn*/
395 class_java_lang_Cloneable =
396 class_new(utf_new_char("java/lang/Cloneable"));
397 class_load(class_java_lang_Cloneable);
398 class_link(class_java_lang_Cloneable);
400 class_java_lang_Class =
401 class_new(utf_new_char("java/lang/Class"));
402 class_load(class_java_lang_Class);
403 class_link(class_java_lang_Class);
405 class_java_lang_VMClass =
406 class_new(utf_new_char("java/lang/VMClass"));
407 class_load(class_java_lang_VMClass);
408 class_link(class_java_lang_VMClass);
410 class_java_lang_ClassLoader =
411 class_new(utf_new_char("java/lang/ClassLoader"));
412 class_load(class_java_lang_ClassLoader);
413 class_link(class_java_lang_ClassLoader);
415 /* load classes for wrapping primitive types */
416 class_java_lang_Double = class_new(utf_new_char("java/lang/Double"));
417 class_load(class_java_lang_Double);
418 class_link(class_java_lang_Double);
420 class_java_lang_Float = class_new(utf_new_char("java/lang/Float"));
421 class_load(class_java_lang_Float);
422 class_link(class_java_lang_Float);
424 class_java_lang_Character = class_new(utf_new_char("java/lang/Character"));
425 class_load(class_java_lang_Character);
426 class_link(class_java_lang_Character);
428 class_java_lang_Integer = class_new(utf_new_char("java/lang/Integer"));
429 class_load(class_java_lang_Integer);
430 class_link(class_java_lang_Integer);
432 class_java_lang_Long = class_new(utf_new_char("java/lang/Long"));
433 class_load(class_java_lang_Long);
434 class_link(class_java_lang_Long);
436 class_java_lang_Byte = class_new(utf_new_char("java/lang/Byte"));
437 class_load(class_java_lang_Byte);
438 class_link(class_java_lang_Byte);
440 class_java_lang_Short = class_new(utf_new_char("java/lang/Short"));
441 class_load(class_java_lang_Short);
442 class_link(class_java_lang_Short);
444 class_java_lang_Boolean = class_new(utf_new_char("java/lang/Boolean"));
445 class_load(class_java_lang_Boolean);
446 class_link(class_java_lang_Boolean);
448 class_java_lang_Void = class_new(utf_new_char("java/lang/Void"));
449 class_load(class_java_lang_Void);
450 class_link(class_java_lang_Void);
454 /*****************************************************************************
456 create systemclassloader object and initialize instance fields
458 ******************************************************************************/
460 void init_systemclassloader()
462 log_text("init_systemclassloader");
463 if (!SystemClassLoader) {
464 native_loadclasses();
465 log_text("Initializing new system class loader");
466 /* create object and call initializer */
467 SystemClassLoader = (java_lang_ClassLoader *) native_new_and_init(class_new(utf_new_char("gnu/java/lang/SystemClassLoader")));
469 /* systemclassloader has no parent */
470 SystemClassLoader->parent = NULL;
471 SystemClassLoader->initialized = true;
473 log_text("leaving system class loader");
477 /********************* function: native_setclasspath **************************/
479 void native_setclasspath(char *path)
481 /* set searchpath for classfiles */
486 /*********************** Function: native_findfunction *************************
488 Looks up a method (must have the same class name, method name, descriptor
489 and 'static'ness) and returns a function pointer to it.
490 Returns: function pointer or NULL (if there is no such method)
492 Remark: For faster operation, the names/descriptors are converted from C
493 strings to Unicode the first time this function is called.
495 *******************************************************************************/
497 functionptr native_findfunction(utf *cname, utf *mname,
498 utf *desc, bool isstatic)
501 /* entry of table for fast string comparison */
502 struct nativecompref *n;
503 /* for warning message if no function is found */
507 isstatic = isstatic ? true : false;
509 if (!nativecompdone) {
510 for (i = 0; i < NATIVETABLESIZE; i++) {
511 nativecomptable[i].classname =
512 utf_new_char(nativetable[i].classname);
513 nativecomptable[i].methodname =
514 utf_new_char(nativetable[i].methodname);
515 nativecomptable[i].descriptor =
516 utf_new_char(nativetable[i].descriptor);
517 nativecomptable[i].isstatic =
518 nativetable[i].isstatic;
519 nativecomptable[i].func =
522 nativecompdone = true;
527 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
529 buffer = MNEW(char, buffer_len);
531 strcpy(buffer, "searching matching function in native table:");
532 utf_sprint(buffer+strlen(buffer), mname);
533 strcpy(buffer+strlen(buffer), ": ");
534 utf_sprint(buffer+strlen(buffer), desc);
535 strcpy(buffer+strlen(buffer), " for class ");
536 utf_sprint(buffer+strlen(buffer), cname);
540 MFREE(buffer, char, buffer_len);
543 for (i = 0; i < NATIVETABLESIZE; i++) {
544 n = &(nativecomptable[i]);
546 if (cname == n->classname && mname == n->methodname &&
547 desc == n->descriptor && isstatic == n->isstatic)
551 if (cname == n->classname && mname == n->methodname ) log_text("static and descriptor mismatch");
555 utf_strlen(n->classname) + utf_strlen(n->methodname) + utf_strlen(n->descriptor) + 64;
557 buffer = MNEW(char, buffer_len);
559 strcpy(buffer, "comparing with:");
560 utf_sprint(buffer+strlen(buffer), n->methodname);
561 strcpy (buffer+strlen(buffer), ": ");
562 utf_sprint(buffer+strlen(buffer), n->descriptor);
563 strcpy(buffer+strlen(buffer), " for class ");
564 utf_sprint(buffer+strlen(buffer), n->classname);
568 MFREE(buffer, char, buffer_len);
575 /* no function was found, display warning */
578 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
580 buffer = MNEW(char, buffer_len);
582 strcpy(buffer, "warning: native function ");
583 utf_sprint(buffer + strlen(buffer), mname);
584 strcpy(buffer + strlen(buffer), ": ");
585 utf_sprint(buffer + strlen(buffer), desc);
586 strcpy(buffer + strlen(buffer), " not found in class ");
587 utf_sprint(buffer + strlen(buffer), cname);
591 MFREE(buffer, char, buffer_len);
595 /* keep compiler happy */
600 /********************** function: javastring_new *******************************
602 creates a new object of type java/lang/String with the text of
603 the specified utf8-string
605 return: pointer to the string or NULL if memory is exhausted.
607 *******************************************************************************/
609 java_lang_String *javastring_new(utf *u)
611 char *utf_ptr = u->text; /* current utf character in utf string */
612 int utflength = utf_strlen(u); /* length of utf-string if uncompressed */
613 java_lang_String *s; /* result-string */
617 s = (java_lang_String *) builtin_new(class_java_lang_String);
618 a = builtin_newarray_char(utflength);
620 /* javastring or character-array could not be created */
624 /* decompress utf-string */
625 for (i = 0; i < utflength; i++)
626 a->data[i] = utf_nextu2(&utf_ptr);
628 /* set fields of the javastring-object */
631 s->count = utflength;
637 /********************** function: javastring_new_char **************************
639 creates a new java/lang/String object which contains the convertet
640 C-string passed via text.
642 return: the object pointer or NULL if memory is exhausted.
644 *******************************************************************************/
646 java_lang_String *javastring_new_char(char *text)
649 s4 len = strlen(text); /* length of the string */
650 java_lang_String *s; /* result-string */
653 s = (java_lang_String *) builtin_new(class_java_lang_String);
654 a = builtin_newarray_char(len);
656 /* javastring or character-array could not be created */
661 for (i = 0; i < len; i++)
662 a->data[i] = text[i];
664 /* set fields of the javastring-object */
673 /************************* function javastring_tochar **************************
675 converts a Java string into a C string.
677 return: pointer to C string
679 Caution: every call of this function overwrites the previous string !!!
681 *******************************************************************************/
683 static char stringbuffer[MAXSTRINGSIZE];
685 char *javastring_tochar(java_objectheader *so)
687 java_lang_String *s = (java_lang_String *) so;
699 if (s->count > MAXSTRINGSIZE)
702 for (i = 0; i < s->count; i++)
703 stringbuffer[i] = a->data[s->offset + i];
705 stringbuffer[i] = '\0';
711 /****************** function class_findfield_approx ****************************
713 searches in 'classinfo'-structure for a field with the
716 *******************************************************************************/
718 fieldinfo *class_findfield_approx(classinfo *c, utf *name)
722 for (i = 0; i < c->fieldscount; i++) {
723 /* compare field names */
724 if ((c->fields[i].name == name))
725 return &(c->fields[i]);
728 /* field was not found, raise exception */
729 *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
735 s4 class_findfield_index_approx(classinfo *c, utf *name)
739 for (i = 0; i < c->fieldscount; i++) {
740 /* compare field names */
741 if ((c->fields[i].name == name))
745 /* field was not found, raise exception */
746 *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
752 /********************** function: native_new_and_init *************************
754 Creates a new object on the heap and calls the initializer.
755 Returns the object pointer or NULL if memory is exhausted.
757 *******************************************************************************/
759 java_objectheader *native_new_and_init(classinfo *c)
762 java_objectheader *o;
765 return *exceptionptr;
767 o = builtin_new(c); /* create object */
772 /* find initializer */
774 m = class_findmethod(c,
775 utf_new_char("<init>"),
776 utf_new_char("()V"));
778 if (!m) { /* initializer not found */
780 char logtext[MAXLOGTEXT];
781 sprintf(logtext, "Warning: class has no instance-initializer: ");
782 utf_sprint_classname(logtext + strlen(logtext), c->name);
788 /* call initializer */
790 asm_calljavafunction(m, o, NULL, NULL, NULL);
796 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
799 java_objectheader *o;
802 return *exceptionptr;
804 o = builtin_new(c); /* create object */
809 /* find initializer */
811 m = class_findmethod(c,
812 utf_new_char("<init>"),
813 utf_new_char("(Ljava/lang/String;)V"));
815 if (!m) { /* initializer not found */
817 char logtext[MAXLOGTEXT];
818 sprintf(logtext, "Warning: class has no instance-initializer: ");
819 utf_sprint_classname(logtext + strlen(logtext), c->name);
825 /* call initializer */
827 asm_calljavafunction(m, o, s, NULL, NULL);
833 java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
836 java_objectheader *o;
839 return *exceptionptr;
841 o = builtin_new(c); /* create object */
845 /* find initializer */
847 m = class_findmethod(c,
848 utf_new_char("<init>"),
849 utf_new_char("(I)V"));
851 if (!m) { /* initializer not found */
853 char logtext[MAXLOGTEXT];
854 sprintf(logtext, "Warning: class has no instance-initializer: ");
855 utf_sprint_classname(logtext + strlen(logtext), c->name);
861 /* call initializer */
863 asm_calljavafunction(m, o, (void *) i, NULL, NULL);
869 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t)
872 java_objectheader *o;
875 return *exceptionptr;
877 o = builtin_new(c); /* create object */
881 /* find initializer */
883 m = class_findmethod(c,
884 utf_new_char("<init>"),
885 utf_new_char("(Ljava/lang/Throwable;)V"));
887 if (!m) { /* initializer not found */
889 char logtext[MAXLOGTEXT];
890 sprintf(logtext, "Warning: class has no instance-initializer: ");
891 utf_sprint_classname(logtext + strlen(logtext), c->name);
897 /* call initializer */
899 asm_calljavafunction(m, o, t, NULL, NULL);
905 /******************** function: stringtable_update ****************************
907 traverses the javastring hashtable and sets the vftbl-entries of
908 javastrings which were temporarily set to NULL, because
909 java.lang.Object was not yet loaded
911 *******************************************************************************/
913 void stringtable_update ()
915 java_lang_String *js;
917 literalstring *s; /* hashtable entry */
920 for (i = 0; i < string_hash.size; i++) {
921 s = string_hash.ptr[i];
925 js = (java_lang_String *) s->string;
927 if (!js || !js->value)
928 /* error in hashtable found */
929 panic("invalid literalstring in hashtable");
933 if (!js->header.vftbl)
934 /* vftbl of javastring is NULL */
935 js->header.vftbl = class_java_lang_String->vftbl;
937 if (!a->header.objheader.vftbl)
938 /* vftbl of character-array is NULL */
939 a->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
941 /* follow link in external hash chain */
949 /************************* function: u2_utflength ***************************
951 returns the utf length in bytes of a u2 array
953 *****************************************************************************/
955 u4 u2_utflength(u2 *text, u4 u2_length)
957 u4 result_len = 0; /* utf length in bytes */
958 u2 ch; /* current unicode character */
961 for (len = 0; len < u2_length; len++) {
962 /* next unicode character */
965 /* determine bytes required to store unicode character as utf */
966 if (ch && (ch < 0x80))
978 /********************* function: utf_new_u2 ***********************************
980 make utf symbol from u2 array,
981 if isclassname is true '.' is replaced by '/'
983 *******************************************************************************/
985 utf *utf_new_u2(u2 *unicode_pos, u4 unicode_length, bool isclassname)
987 char *buffer; /* memory buffer for unicode characters */
988 char *pos; /* pointer to current position in buffer */
989 u4 left; /* unicode characters left */
990 u4 buflength; /* utf length in bytes of the u2 array */
991 utf *result; /* resulting utf-string */
994 /* determine utf length in bytes and allocate memory */
995 /* printf("utf_new_u2: unicode_length=%d\n",unicode_length); */
996 buflength = u2_utflength(unicode_pos, unicode_length);
997 buffer = MNEW(char, buflength);
999 /* memory allocation failed */
1001 printf("length: %d\n",buflength);
1002 log_text("utf_new_u2:buffer==NULL");
1008 for (i = 0; i++ < unicode_length; unicode_pos++) {
1009 /* next unicode character */
1010 u2 c = *unicode_pos;
1012 if ((c != 0) && (c < 0x80)) {
1015 if ((int) left < 0) break;
1016 /* convert classname */
1017 if (isclassname && c == '.')
1022 } else if (c < 0x800) {
1024 unsigned char high = c >> 6;
1025 unsigned char low = c & 0x3F;
1027 if ((int) left < 0) break;
1028 *pos++ = high | 0xC0;
1029 *pos++ = low | 0x80;
1033 char low = c & 0x3f;
1034 char mid = (c >> 6) & 0x3F;
1035 char high = c >> 12;
1037 if ((int) left < 0) break;
1038 *pos++ = high | 0xE0;
1039 *pos++ = mid | 0x80;
1040 *pos++ = low | 0x80;
1044 /* insert utf-string into symbol-table */
1045 result = utf_new(buffer,buflength);
1047 MFREE(buffer, char, buflength);
1053 /********************* function: javastring_toutf *****************************
1055 make utf symbol from javastring
1057 *******************************************************************************/
1059 utf *javastring_toutf(java_lang_String *string, bool isclassname)
1061 java_lang_String *str = (java_lang_String *) string;
1063 /* printf("javastring_toutf offset: %d, len %d\n",str->offset, str->count); */
1064 /* fflush(stdout); */
1066 return utf_new_u2(str->value->data + str->offset, str->count, isclassname);
1070 /********************* function: literalstring_u2 *****************************
1072 searches for the javastring with the specified u2-array in
1073 the string hashtable, if there is no such string a new one is
1076 if copymode is true a copy of the u2-array is made
1078 *******************************************************************************/
1080 java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset,
1083 literalstring *s; /* hashtable element */
1084 java_lang_String *js; /* u2-array wrapped in javastring */
1085 java_chararray *stringdata; /* copy of u2-array */
1090 //#define DEBUG_LITERALSTRING_U2
1091 #ifdef DEBUG_LITERALSTRING_U2
1092 printf("literalstring_u2: length=%d, offset=%d\n", length, offset);
1096 /* find location in hashtable */
1097 key = unicode_hashkey(a->data + offset, length);
1098 slot = key & (string_hash.size - 1);
1099 s = string_hash.ptr[slot];
1102 js = (java_lang_String *) s->string;
1104 if (length == js->count) {
1106 for (i = 0; i < length; i++) {
1107 if (a->data[offset + i] != js->value->data[i])
1111 /* string already in hashtable, free memory */
1113 lit_mem_free(a, sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10);
1115 #ifdef DEBUG_LITERALSTRING_U2
1116 printf("literalstring_u2: foundentry at %p\n", js);
1117 utf_display(javastring_toutf(js, 0));
1121 return (java_objectheader *) js;
1125 /* follow link in external hash chain */
1130 /* create copy of u2-array for new javastring */
1131 u4 arraysize = sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10;
1132 stringdata = lit_mem_alloc(arraysize);
1133 /* memcpy(stringdata, a, arraysize); */
1134 memcpy(&(stringdata->header), &(a->header), sizeof(java_arrayheader));
1135 memcpy(&(stringdata->data), &(a->data) + offset, sizeof(u2) * (length - 1) + 10);
1141 /* location in hashtable found, complete arrayheader */
1142 stringdata->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
1143 stringdata->header.size = length;
1145 /* create new javastring */
1146 js = LNEW(java_lang_String);
1148 /* js->header.vftbl = class_java_lang_String->vftbl; */
1149 js->header.vftbl = class_load(class_new(utf_new_char("java/lang/String")))->vftbl;
1150 js->value = stringdata;
1154 /* create new literalstring */
1155 s = NEW(literalstring);
1156 s->hashlink = string_hash.ptr[slot];
1157 s->string = (java_objectheader *) js;
1158 string_hash.ptr[slot] = s;
1160 /* update numbe of hashtable entries */
1161 string_hash.entries++;
1163 /* reorganization of hashtable */
1164 if (string_hash.entries > (string_hash.size * 2)) {
1165 /* reorganization of hashtable, average length of
1166 the external chains is approx. 2 */
1170 hashtable newhash; /* the new hashtable */
1172 /* create new hashtable, double the size */
1173 init_hashtable(&newhash, string_hash.size * 2);
1174 newhash.entries = string_hash.entries;
1176 /* transfer elements to new hashtable */
1177 for (i = 0; i < string_hash.size; i++) {
1178 s = string_hash.ptr[i];
1180 literalstring *nexts = s->hashlink;
1181 js = (java_lang_String *) s->string;
1182 slot = unicode_hashkey(js->value->data, js->count) & (newhash.size - 1);
1184 s->hashlink = newhash.ptr[slot];
1185 newhash.ptr[slot] = s;
1187 /* follow link in external hash chain */
1192 /* dispose old table */
1193 MFREE(string_hash.ptr, void*, string_hash.size);
1194 string_hash = newhash;
1197 #ifdef DEBUG_LITERALSTRING_U2
1198 printf("literalstring_u2: newly created at %p\n", js);
1199 utf_display(javastring_toutf(js, 0));
1204 return (java_objectheader *) js;
1208 /******************** Function: literalstring_new *****************************
1210 creates a new javastring with the text of the utf-symbol
1211 and inserts it into the string hashtable
1213 *******************************************************************************/
1215 java_objectheader *literalstring_new(utf *u)
1217 char *utf_ptr = u->text; /* pointer to current unicode character in utf string */
1218 u4 utflength = utf_strlen(u); /* length of utf-string if uncompressed */
1219 java_chararray *a; /* u2-array constructed from utf string */
1222 /* allocate memory */
1223 a = lit_mem_alloc(sizeof(java_chararray) + sizeof(u2) * (utflength - 1) + 10);
1225 /* convert utf-string to u2-array */
1226 for (i = 0; i < utflength; i++)
1227 a->data[i] = utf_nextu2(&utf_ptr);
1229 return literalstring_u2(a, utflength, 0, false);
1233 /********************** function: literalstring_free **************************
1235 removes a javastring from memory
1237 ******************************************************************************/
1239 void literalstring_free(java_objectheader* sobj)
1241 java_lang_String *s = (java_lang_String *) sobj;
1242 java_chararray *a = s->value;
1244 /* dispose memory of java.lang.String object */
1245 LFREE(s, java_lang_String);
1247 /* dispose memory of java-characterarray */
1248 LFREE(a, sizeof(java_chararray) + sizeof(u2) * (a->header.size - 1)); /* +10 ?? */
1252 void copy_vftbl(vftbl **dest, vftbl *src)
1256 /* XXX this kind of copying does not work (in the general
1257 * case). The interface tables would have to be copied, too. I
1258 * don't see why we should make a copy anyway. -Edwin
1260 *dest = mem_alloc(sizeof(vftbl) + sizeof(methodptr)*(src->vftbllength-1));
1261 memcpy(*dest, src, sizeof(vftbl) - sizeof(methodptr));
1262 memcpy(&(*dest)->table, &src->table, src->vftbllength * sizeof(methodptr));
1267 /******************************************************************************************
1269 creates method signature (excluding return type) from array of
1270 class-objects representing the parameters of the method
1272 *******************************************************************************************/
1275 utf *create_methodsig(java_objectarray* types, char *retType)
1277 char *buffer; /* buffer for building the desciptor */
1278 char *pos; /* current position in buffer */
1279 utf *result; /* the method signature */
1280 u4 buffer_size = 3; /* minimal size=3: room for parenthesis and returntype */
1283 if (!types) return NULL;
1285 /* determine required buffer-size */
1286 for (i = 0; i < types->header.size; i++) {
1287 classinfo *c = (classinfo *) types->data[i];
1288 buffer_size = buffer_size + c->name->blength + 2;
1291 if (retType) buffer_size += strlen(retType);
1293 /* allocate buffer */
1294 buffer = MNEW(u1, buffer_size);
1297 /* method-desciptor starts with parenthesis */
1300 for (i = 0; i < types->header.size; i++) {
1303 /* current argument */
1304 classinfo *c = (classinfo *) types->data[i];
1306 /* current position in utf-text */
1307 char *utf_ptr = c->name->text;
1309 /* determine type of argument */
1310 if ((ch = utf_nextu2(&utf_ptr)) == '[') {
1312 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1313 *pos++ = *utf_ptr; /* copy text */
1317 /* check for primitive types */
1318 for (j = 0; j < PRIMITIVETYPE_COUNT; j++) {
1319 char *utf_pos = utf_ptr - 1;
1320 char *primitive = primitivetype_table[j].wrapname;
1323 while (utf_pos < utf_end(c->name)) {
1324 if (*utf_pos++ != *primitive++) goto nomatch;
1327 /* primitive type found */
1328 *pos++ = primitivetype_table[j].typesig;
1335 /* no primitive type and no arrayclass, so must be object */
1339 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1353 for (i = 0; i < strlen(retType); i++) {
1354 *pos++ = retType[i];
1358 /* create utf-string */
1359 result = utf_new(buffer, (pos - buffer));
1360 MFREE(buffer, u1, buffer_size);
1366 /******************************************************************************************
1368 retrieve the next argument or returntype from a descriptor
1369 and return the corresponding class
1371 *******************************************************************************************/
1373 classinfo *get_type(char **utf_ptr,char *desc_end, bool skip)
1375 classinfo *c = class_from_descriptor(*utf_ptr,desc_end,utf_ptr,
1376 (skip) ? CLASSLOAD_SKIP : CLASSLOAD_LOAD);
1379 panic("illegal descriptor");
1381 if (skip) return NULL;
1383 use_class_as_object(c);
1388 /******************************************************************************************
1390 use the descriptor of a method to generate a java/lang/Class array
1391 which contains the classes of the parametertypes of the method
1393 *******************************************************************************************/
1395 java_objectarray* get_parametertypes(methodinfo *m)
1397 utf *descr = m->descriptor; /* method-descriptor */
1398 char *utf_ptr = descr->text; /* current position in utf-text */
1399 char *desc_end = utf_end(descr); /* points behind utf string */
1400 java_objectarray* result;
1401 int parametercount = 0;
1405 utf_nextu2(&utf_ptr);
1407 /* determine number of parameters */
1408 while ( *utf_ptr != ')' ) {
1409 get_type(&utf_ptr,desc_end,true);
1413 /* create class-array */
1414 result = builtin_anewarray(parametercount, class_java_lang_Class);
1416 utf_ptr = descr->text;
1417 utf_nextu2(&utf_ptr);
1419 /* get returntype classes */
1420 for (i = 0; i < parametercount; i++)
1421 result->data[i] = (java_objectheader *) get_type(&utf_ptr,desc_end, false);
1430 /******************************************************************************************
1432 get the exceptions which can be thrown by a method
1434 *******************************************************************************************/
1436 java_objectarray* get_exceptiontypes(methodinfo *m) {
1437 u2 exccount=m->thrownexceptionscount;
1439 java_objectarray *result;
1440 /* create class-array */
1441 result = builtin_anewarray(exccount, class_java_lang_Class);
1442 for (i=0;i<exccount;i++) {
1443 java_objectheader *oh=(java_objectheader*)(m->thrownexceptions[i]);
1444 use_class_as_object(oh);
1454 /******************************************************************************************
1456 get the returntype class of a method
1458 *******************************************************************************************/
1460 classinfo *get_returntype(methodinfo *m)
1462 char *utf_ptr; /* current position in utf-text */
1463 char *desc_end; /* points behind utf string */
1464 utf *desc = m->descriptor; /* method-descriptor */
1466 utf_ptr = desc->text;
1467 desc_end = utf_end(desc);
1469 /* ignore parametertypes */
1470 while ((utf_ptr<desc_end) && utf_nextu2(&utf_ptr)!=')')
1473 return get_type(&utf_ptr,desc_end, false);
1477 /*****************************************************************************/
1478 /*****************************************************************************/
1481 /*--------------------------------------------------------*/
1482 void printNativeCall(nativeCall nc) {
1485 printf("\n%s's Native Methods call:\n",nc.classname); fflush(stdout);
1486 for (i=0; i<nc.methCnt; i++) {
1487 printf("\tMethod=%s %s\n",nc.methods[i].methodname, nc.methods[i].descriptor);fflush(stdout);
1489 for (j=0; j<nc.callCnt[i]; j++) {
1490 printf("\t\t<%i,%i>aCalled = %s %s %s\n",i,j,
1491 nc.methods[i].methodCalls[j].classname,
1492 nc.methods[i].methodCalls[j].methodname,
1493 nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1496 printf("-+++++--------------------\n");fflush(stdout);
1499 /*--------------------------------------------------------*/
1500 void printCompNativeCall(nativeCompCall nc) {
1502 printf("printCompNativeCall BEGIN\n");fflush(stdout);
1503 printf("\n%s's Native Comp Methods call:\n",nc.classname->text);fflush(stdout);
1504 utf_display(nc.classname); fflush(stdout);
1506 for (i=0; i<nc.methCnt; i++) {
1507 printf("\tMethod=%s %s\n",nc.methods[i].methodname->text,nc.methods[i].descriptor->text);fflush(stdout);
1508 utf_display(nc.methods[i].methodname); fflush(stdout);
1509 utf_display(nc.methods[i].descriptor);fflush(stdout);
1510 printf("\n");fflush(stdout);
1512 for (j=0; j<nc.callCnt[i]; j++) {
1513 printf("\t\t<%i,%i>bCalled = ",i,j);fflush(stdout);
1514 utf_display(nc.methods[i].methodCalls[j].classname);fflush(stdout);
1515 utf_display(nc.methods[i].methodCalls[j].methodname); fflush(stdout);
1516 utf_display(nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1517 printf("\n");fflush(stdout);
1520 printf("---------------------\n");fflush(stdout);
1524 /*--------------------------------------------------------*/
1525 classMeth findNativeMethodCalls(utf *c, utf *m, utf *d )
1538 /*--------------------------------------------------------*/
1539 nativeCall* findNativeClassCalls(char *aclassname ) {
1542 for (i=0;i<NATIVECALLSSIZE; i++) {
1543 /* convert table to utf later to speed up search */
1544 if (strcmp(nativeCalls[i].classname, aclassname) == 0)
1545 return &nativeCalls[i];
1550 /*--------------------------------------------------------*/
1551 /*--------------------------------------------------------*/
1552 void utfNativeCall(nativeCall nc, nativeCompCall *ncc) {
1556 ncc->classname = utf_new_char(nc.classname);
1557 ncc->methCnt = nc.methCnt;
1559 for (i=0; i<nc.methCnt; i++) {
1560 ncc->methods[i].methodname = utf_new_char(nc.methods[i].methodname);
1561 ncc->methods[i].descriptor = utf_new_char(nc.methods[i].descriptor);
1562 ncc->callCnt[i] = nc.callCnt[i];
1564 for (j=0; j<nc.callCnt[i]; j++) {
1566 ncc->methods[i].methodCalls[j].classname = utf_new_char(nc.methods[i].methodCalls[j].classname);
1568 if (strcmp("", nc.methods[i].methodCalls[j].methodname) != 0) {
1569 ncc->methods[i].methodCalls[j].methodname = utf_new_char(nc.methods[i].methodCalls[j].methodname);
1570 ncc->methods[i].methodCalls[j].descriptor = utf_new_char(nc.methods[i].methodCalls[j].descriptor);
1573 ncc->methods[i].methodCalls[j].methodname = NULL;
1574 ncc->methods[i].methodCalls[j].descriptor = NULL;
1582 /*--------------------------------------------------------*/
1584 bool natcall2utf(bool natcallcompdone) {
1587 if (natcallcompdone)
1590 for (i=0;i<NATIVECALLSSIZE; i++) {
1591 utfNativeCall (nativeCalls[i], &nativeCompCalls[i]);
1597 /*--------------------------------------------------------*/
1600 java_objectarray *builtin_asm_createclasscontextarray(classinfo **end,classinfo **start) {
1601 #warning platform dependend
1602 java_objectarray *tmpArray;
1604 classinfo **current;
1606 size_t size=(((size_t)start)-((size_t)end)) / sizeof (classinfo*);
1607 /* printf("end %p, start %p, size %ld\n",end,start,size);*/
1608 if (!class_java_lang_Class)
1609 class_java_lang_Class = class_new(utf_new_char ("java/lang/Class"));
1610 if (!class_java_lang_SecurityManager)
1611 class_java_lang_SecurityManager = class_new(utf_new_char ("java/lang/SecurityManager"));
1613 if (start==class_java_lang_SecurityManager) {
1618 tmpArray=builtin_newarray(size, class_array_of(class_java_lang_Class)->vftbl);
1620 for(i=0,current=start;i<size;i++,current--) {
1622 /* printf("%d\n",i);
1623 utf_display(c->name);*/
1624 use_class_as_object(c);
1625 tmpArray->data[i]=c;
1631 java_lang_ClassLoader *builtin_asm_getclassloader(classinfo **end,classinfo **start) {
1632 #warning platform dependend
1634 classinfo **current;
1636 classinfo *privilegedAction;
1637 size_t size=(((size_t)start)-((size_t)end)) / sizeof (classinfo*);
1638 /* log_text("builtin_asm_getclassloader");
1639 printf("end %p, start %p, size %ld\n",end,start,size);*/
1641 if (!class_java_lang_SecurityManager)
1642 class_java_lang_SecurityManager = class_new(utf_new_char ("java/lang/SecurityManager"));
1644 if (start==class_java_lang_SecurityManager) {
1650 privilegedAction=class_new(utf_new_char("java/security/PrivilegedAction"));
1652 for(i=0,current=start;i<size;i++,current--) {
1654 if (c==privilegedAction) return NULL;
1655 if (c->classloader) return c->classloader;
1663 log_text("Java_java_lang_VMSecurityManager_currentClassLoader");
1664 init_systemclassloader();
1666 return SystemClassLoader;*/
1670 * These are local overrides for various environment variables in Emacs.
1671 * Please do not remove this and leave it at the end of the file, where
1672 * Emacs will automagically detect them.
1673 * ---------------------------------------------------------------------
1676 * indent-tabs-mode: t