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 934 2004-03-05 23:20:53Z twisti $
52 #include "nativetypes.hh"
58 #include "toolbox/loging.h"
59 #include "toolbox/memory.h"
60 #include "threads/thread.h"
61 #include "threads/threadio.h"
62 #include "threads/locks.h"
64 /* Include files for IO functions */
68 #include <sys/types.h>
74 #include "threads/threadio.h"
76 /* searchpath for classfiles */
79 /* for java-string to char conversion */
80 #define MAXSTRINGSIZE 1000
82 /******************** systemclasses required for native methods ***************/
84 classinfo *class_java_lang_Class;
85 classinfo *class_java_lang_VMClass;
86 methodinfo *method_vmclass_init;
87 /* static classinfo *class_java_lang_Cloneable=0; */ /* now in global.h */
88 classinfo *class_java_lang_CloneNotSupportedException;
89 classinfo *class_java_lang_System;
90 classinfo *class_java_lang_ClassLoader;
91 classinfo *class_gnu_java_lang_SystemClassLoader;
92 classinfo *class_java_lang_NoClassDefFoundError;
93 classinfo *class_java_lang_ClassNotFoundException;
94 classinfo *class_java_lang_LinkageError;
95 classinfo *class_java_lang_InstantiationException;
96 classinfo *class_java_lang_NoSuchMethodError;
97 classinfo *class_java_lang_NoSuchFieldError;
98 classinfo *class_java_lang_ClassFormatError;
99 classinfo *class_java_lang_IllegalArgumentException;
100 classinfo *class_java_lang_ArrayIndexOutOfBoundsException;
101 classinfo *class_java_lang_NoSuchFieldException;
102 classinfo *class_java_io_SyncFailedException;
103 classinfo *class_java_io_IOException;
104 classinfo *class_java_io_FileNotFoundException;
105 classinfo *class_java_io_UnixFileSystem;
106 classinfo *class_java_security_PrivilegedActionException;
107 classinfo *class_java_lang_SecurityManager;
108 classinfo *class_java_net_UnknownHostException;
109 classinfo *class_java_net_SocketException;
110 classinfo *class_java_lang_NoSuchMethodException;
111 classinfo *class_java_lang_Double;
112 classinfo *class_java_lang_Float;
113 classinfo *class_java_lang_Long;
114 classinfo *class_java_lang_Byte;
115 classinfo *class_java_lang_Short;
116 classinfo *class_java_lang_Boolean;
117 classinfo *class_java_lang_Void;
118 classinfo *class_java_lang_Character;
119 classinfo *class_java_lang_Integer;
122 /* specify some exception strings for code generation */
123 char *string_java_lang_NoClassDefFoundError =
124 "java/lang/NoClassDefFoundError";
126 char *string_java_lang_LinkageError =
127 "java/lang/LinkageError";
129 char *string_java_lang_ArrayIndexOutOfBoundsException =
130 "java/lang/ArrayIndexOutOfBoundsException";
132 char *string_java_lang_NegativeArraySizeException =
133 "java/lang/NegativeArraySizeException";
135 char *string_java_lang_ClassCastException =
136 "java/lang/ClassCastException";
138 char *string_java_lang_ArithmeticException =
139 "java/lang/ArithmeticException";
141 char *string_java_lang_ArithmeticException_message =
144 char *string_java_lang_NullPointerException =
145 "java/lang/NullPointerException";
147 char *string_java_lang_ArrayStoreException =
148 "java/lang/ArrayStoreException";
151 /* the system classloader object */
152 struct java_lang_ClassLoader *SystemClassLoader = NULL;
154 /* for raising exceptions from native methods */
155 THREADSPECIFIC java_objectheader* _exceptionptr = NULL;
157 /************* use classinfo structure as java.lang.Class object **************/
159 void use_class_as_object(classinfo *c)
164 if (!class_java_lang_Class)
165 class_java_lang_Class = class_new(utf_new_char ("java/lang/Class"));
167 vt = class_java_lang_Class->vftbl;
170 if (!c->classvftbl) {
171 c->classvftbl = true;
173 /* copy_vftbl(&newtbl, vt);
174 newtbl->class = c->header.vftbl->class;
175 newtbl->baseval = c->header.vftbl->baseval;
176 newtbl->diffval = c->header.vftbl->diffval;
177 c->header.vftbl = newtbl;*/
179 c->header.vftbl = class_java_lang_Class->vftbl;
181 if (!class_java_lang_VMClass) {
182 loader_load_sysclass(&class_java_lang_VMClass,
183 utf_new_char("java/lang/VMClass"));
185 method_vmclass_init =
186 class_findmethod(class_java_lang_VMClass,
187 utf_new_char("<init>"),
188 utf_new_char("(Lgnu/classpath/RawData;)V"));
190 if (method_vmclass_init == 0) {
191 class_showmethods(class_java_lang_VMClass);
192 panic("Needed class initializer for VMClass could not be found");
196 java_objectheader *vmo = builtin_new(class_java_lang_VMClass);
198 if (!vmo) panic("Error while creating instance of java/lang/VMClass");
199 asm_calljavafunction(method_vmclass_init, vmo, c, NULL, NULL);
200 c->vmClass = (java_lang_VMClass *) vmo;
201 /*log_text("VMCLASS has been attached");*/
207 /*************************** include native methods ***************************/
210 #include "nat/GdkGraphics.c"
211 #include "nat/GtkComponentPeer.c"
212 #include "nat/GdkPixbufDecoder.c"
213 #include "nat/GtkScrollPanePeer.c"
214 #include "nat/GtkFileDialogPeer.c"
215 #include "nat/GtkLabelPeer.c"
219 /************************** tables for methods ********************************/
224 /* table for locating native methods */
225 static struct nativeref {
233 #include "nativetable.hh"
238 #define NATIVETABLESIZE (sizeof(nativetable)/sizeof(struct nativeref))
240 /* table for fast string comparison */
241 static struct nativecompref {
247 } nativecomptable [NATIVETABLESIZE];
249 /* string comparsion table initialized */
250 static bool nativecompdone = false;
253 /******************************************************************************/
254 /******************************************************************************/
255 #include "natcalls.h"
257 /* string call comparison table initialized */
259 /******************************************************************************/
260 /******************************************************************************/
262 /*--------------- native method calls & classes used -------------------------*/
266 /* throw some loader exceptions */
268 void throw_noclassdeffounderror_message(utf* classname)
270 if (!class_java_lang_NoClassDefFoundError) {
271 panic("java.lang.NoClassDefFoundError not found. Maybe wrong classpath?");
274 /* throws a NoClassDefFoundError with message */
275 *exceptionptr = native_new_and_init_string(class_java_lang_NoClassDefFoundError,
276 javastring_new(classname));
280 void throw_linkageerror_message(utf* classname)
282 if (!class_java_lang_LinkageError) {
283 panic("java.lang.LinkageError not found. Maybe wrong classpath?");
286 /* throws a LinkageError with message */
287 *exceptionptr = native_new_and_init_string(class_java_lang_LinkageError,
288 javastring_new(classname));
292 java_objectheader *new_exception(char *classname)
294 classinfo *c = class_new(utf_new_char(classname));
297 panic("exception class not linked");
299 return native_new_and_init(c);
303 java_objectheader *new_exception_message(char *classname, char *message)
305 classinfo *c = class_new(utf_new_char(classname));
308 panic("exception class not linked");
310 return native_new_and_init_string(c, javastring_new_char(message));
314 java_objectheader *new_exception_int(char *classname, s4 i)
316 classinfo *c = class_new(utf_new_char(classname));
319 panic("exception class not linked");
321 return native_new_and_init_int(c, i);
325 /*********************** function: native_loadclasses **************************
327 load classes required for native methods
329 *******************************************************************************/
331 void native_loadclasses()
333 static int classesLoaded=0; /*temporary hack JoWenn*/
334 if (classesLoaded) return;
336 /* log_text("loadclasses entered");*/
339 /*class_java_lang_System =*/
340 (void) class_new(utf_new_char("java/lang/VMClass"));/*JoWenn*/
341 (void) class_new(utf_new_char("java/lang/Class"));/*JoWenn*/
343 /* class_new adds the class to the list of classes to be loaded */
344 if (!class_java_lang_Cloneable)
345 class_java_lang_Cloneable =
346 class_new(utf_new_char("java/lang/Cloneable"));
347 /* log_text("loadclasses: class_java_lang_Cloneable has been initialized");*/
348 class_java_lang_CloneNotSupportedException =
349 class_new(utf_new_char("java/lang/CloneNotSupportedException"));
350 if (!class_java_lang_Class)
351 class_java_lang_Class =
352 class_new(utf_new_char("java/lang/Class"));
353 class_java_io_IOException =
354 class_new(utf_new_char("java/io/IOException"));
355 class_java_io_FileNotFoundException =
356 class_new(utf_new_char("java/io/FileNotFoundException"));
357 class_java_lang_NoClassDefFoundError =
358 class_new(utf_new_char("java/lang/NoClassDefFoundError"));
359 class_java_lang_ClassNotFoundException =
360 class_new(utf_new_char("java/lang/ClassNotFoundException"));
361 class_java_lang_LinkageError =
362 class_new(utf_new_char("java/lang/LinkageError"));
363 class_java_lang_InstantiationException =
364 class_new(utf_new_char("java/lang/InstantiationException"));
365 class_java_lang_NoSuchMethodError =
366 class_new(utf_new_char("java/lang/NoSuchMethodError"));
367 class_java_lang_NoSuchFieldError =
368 class_new(utf_new_char("java/lang/NoSuchFieldError"));
369 class_java_lang_ClassFormatError =
370 class_new(utf_new_char("java/lang/ClassFormatError"));
371 class_java_io_SyncFailedException =
372 class_new(utf_new_char("java/io/SyncFailedException"));
374 /* log_text("native_loadclasses: class_new(\"java/lang/ClassLoader\")"); */
375 class_java_lang_ClassLoader =
376 class_new(utf_new_char("java/lang/ClassLoader"));
377 class_gnu_java_lang_SystemClassLoader =
378 class_new(utf_new_char("gnu/java/lang/SystemClassLoader"));
380 /* log_text("native_loadclasses: class_new(\"java/security/PrivilegedActionException\")"); */
381 class_java_security_PrivilegedActionException =
382 class_new(utf_new_char("java/security/PrivilegedActionException"));
384 loader_load_sysclass(&class_java_net_UnknownHostException,
385 utf_new_char("java/net/UnknownHostException"));
386 loader_load_sysclass(&class_java_net_SocketException,
387 utf_new_char("java/net/SocketException"));
389 class_java_lang_IllegalArgumentException =
390 class_new(utf_new_char("java/lang/IllegalArgumentException"));
391 class_java_lang_ArrayIndexOutOfBoundsException =
392 class_new(utf_new_char("java/lang/ArrayIndexOutOfBoundsException"));
393 class_java_lang_NoSuchFieldException =
394 class_new(utf_new_char("java/lang/NoSuchFieldException"));
395 class_java_lang_NoSuchMethodException =
396 class_new(utf_new_char("java/lang/NoSuchMethodException"));
398 /* load classes for wrapping primitive types */
399 class_java_lang_Double = class_new(utf_new_char("java/lang/Double"));
400 class_init(class_java_lang_Double);
402 class_java_lang_Float = class_new(utf_new_char("java/lang/Float"));
403 class_java_lang_Character = class_new(utf_new_char("java/lang/Character"));
404 class_java_lang_Integer = class_new(utf_new_char("java/lang/Integer"));
405 class_java_lang_Long = class_new(utf_new_char("java/lang/Long"));
406 class_java_lang_Byte = class_new(utf_new_char("java/lang/Byte"));
407 class_java_lang_Short = class_new(utf_new_char("java/lang/Short"));
408 class_java_lang_Boolean = class_new(utf_new_char("java/lang/Boolean"));
409 class_java_lang_Void = class_new(utf_new_char("java/lang/Void"));
415 /*************** adds a class to the vector of loaded classes ****************/
417 void systemclassloader_addclass(classinfo *c)
421 /* find method addClass of java.lang.ClassLoader */
422 m = class_resolvemethod(class_java_lang_ClassLoader,
423 utf_new_char("addClass"),
424 utf_new_char("(Ljava/lang/Class;)")
428 panic("warning: cannot initialize classloader");
430 /* prepare class to be passed as argument */
431 use_class_as_object (c);
433 /* call 'addClass' */
434 asm_calljavafunction(m,
435 (java_objectheader*) SystemClassLoader,
436 (java_objectheader*) c,
443 /*************** adds a library to the vector of loaded libraries *************/
445 void systemclassloader_addlibrary(java_objectheader *o)
447 log_text("systemclassloader_addlibrary");
451 /*****************************************************************************
453 create systemclassloader object and initialize instance fields
455 ******************************************************************************/
457 void init_systemclassloader()
459 if (!SystemClassLoader) {
460 native_loadclasses();
461 log_text("Initializing new system class loader");
462 /* create object and call initializer */
463 SystemClassLoader = (java_lang_ClassLoader *) native_new_and_init(class_gnu_java_lang_SystemClassLoader);/*class_java_lang_ClassLoader);*/
465 /* systemclassloader has no parent */
466 SystemClassLoader->parent = NULL;
467 SystemClassLoader->initialized = true;
469 log_text("leaving system class loader");
473 /********************* add loaded library name *******************************/
475 void systemclassloader_addlibname(java_objectheader *o)
480 m = class_resolvemethod(loader_load_sysclass(NULL, utf_new_char("java/util/Vector")),
481 utf_new_char("addElement"),
482 utf_new_char("(Ljava/lang/Object;)V"));
484 if (!m) panic("cannot initialize classloader");
486 id = envTable.GetStaticFieldID(&env,
487 class_java_lang_ClassLoader,
488 "loadedLibraryNames",
489 "Ljava/util/Vector;");
491 if (!id) panic("can not access ClassLoader");
493 asm_calljavafunction(m,
494 envTable.GetStaticObjectField(&env, class_java_lang_ClassLoader, id),
501 /********************* function: native_setclasspath **************************/
503 void native_setclasspath(char *path)
505 /* set searchpath for classfiles */
510 /*********************** Function: native_findfunction *************************
512 Looks up a method (must have the same class name, method name, descriptor
513 and 'static'ness) and returns a function pointer to it.
514 Returns: function pointer or NULL (if there is no such method)
516 Remark: For faster operation, the names/descriptors are converted from C
517 strings to Unicode the first time this function is called.
519 *******************************************************************************/
521 functionptr native_findfunction(utf *cname, utf *mname,
522 utf *desc, bool isstatic)
525 /* entry of table for fast string comparison */
526 struct nativecompref *n;
527 /* for warning message if no function is found */
531 isstatic = isstatic ? true : false;
533 if (!nativecompdone) {
534 for (i = 0; i < NATIVETABLESIZE; i++) {
535 nativecomptable[i].classname =
536 utf_new_char(nativetable[i].classname);
537 nativecomptable[i].methodname =
538 utf_new_char(nativetable[i].methodname);
539 nativecomptable[i].descriptor =
540 utf_new_char(nativetable[i].descriptor);
541 nativecomptable[i].isstatic =
542 nativetable[i].isstatic;
543 nativecomptable[i].func =
546 nativecompdone = true;
551 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
553 buffer = MNEW(char, buffer_len);
555 strcpy(buffer, "searching matching function in native table:");
556 utf_sprint(buffer+strlen(buffer), mname);
557 strcpy(buffer+strlen(buffer), ": ");
558 utf_sprint(buffer+strlen(buffer), desc);
559 strcpy(buffer+strlen(buffer), " for class ");
560 utf_sprint(buffer+strlen(buffer), cname);
564 MFREE(buffer, char, buffer_len);
567 for (i = 0; i < NATIVETABLESIZE; i++) {
568 n = &(nativecomptable[i]);
570 if (cname == n->classname && mname == n->methodname &&
571 desc == n->descriptor && isstatic == n->isstatic)
575 if (cname == n->classname && mname == n->methodname ) log_text("static and descriptor mismatch");
579 utf_strlen(n->classname) + utf_strlen(n->methodname) + utf_strlen(n->descriptor) + 64;
581 buffer = MNEW(char, buffer_len);
583 strcpy(buffer, "comparing with:");
584 utf_sprint(buffer+strlen(buffer), n->methodname);
585 strcpy (buffer+strlen(buffer), ": ");
586 utf_sprint(buffer+strlen(buffer), n->descriptor);
587 strcpy(buffer+strlen(buffer), " for class ");
588 utf_sprint(buffer+strlen(buffer), n->classname);
592 MFREE(buffer, char, buffer_len);
599 /* no function was found, display warning */
602 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
604 buffer = MNEW(char, buffer_len);
606 strcpy(buffer, "warning: native function ");
607 utf_sprint(buffer + strlen(buffer), mname);
608 strcpy(buffer + strlen(buffer), ": ");
609 utf_sprint(buffer + strlen(buffer), desc);
610 strcpy(buffer + strlen(buffer), " not found in class ");
611 utf_sprint(buffer + strlen(buffer), cname);
615 MFREE(buffer, char, buffer_len);
619 /* keep compiler happy */
624 /********************** function: javastring_new *******************************
626 creates a new object of type java/lang/String with the text of
627 the specified utf8-string
629 return: pointer to the string or NULL if memory is exhausted.
631 *******************************************************************************/
633 /* java_objectheader *javastring_new(utf *u) */
634 java_lang_String *javastring_new(utf *u)
636 char *utf_ptr = u->text; /* current utf character in utf string */
637 int utflength = utf_strlen(u); /* length of utf-string if uncompressed */
638 java_lang_String *s; /* result-string */
642 /* log_text("javastring_new");*/
644 s = (java_lang_String*) builtin_new(class_java_lang_String);
645 a = builtin_newarray_char(utflength);
647 /* javastring or character-array could not be created */
651 /* decompress utf-string */
652 for (i = 0; i < utflength; i++)
653 a->data[i] = utf_nextu2(&utf_ptr);
655 /* set fields of the javastring-object */
658 s->count = utflength;
660 /* return (java_objectheader*) s; */
665 /********************** function: javastring_new_char **************************
667 creates a new java/lang/String object which contains the convertet
668 C-string passed via text.
670 return: the object pointer or NULL if memory is exhausted.
672 *******************************************************************************/
674 java_lang_String *javastring_new_char(char *text)
677 s4 len = strlen(text); /* length of the string */
678 java_lang_String *s; /* result-string */
681 /*log_text("javastring_new_char");*/
682 s = (java_lang_String *) builtin_new(class_java_lang_String);
683 a = builtin_newarray_char(len);
685 /* javastring or character-array could not be created */
690 for (i = 0; i < len; i++)
691 a->data[i] = text[i];
693 /* set fields of the javastring-object */
698 /* return (java_objectheader*) s; */
703 /************************* function javastring_tochar **************************
705 converts a Java string into a C string.
707 return: pointer to C string
709 Caution: every call of this function overwrites the previous string !!!
711 *******************************************************************************/
713 static char stringbuffer[MAXSTRINGSIZE];
715 char *javastring_tochar(java_objectheader *so)
717 java_lang_String *s = (java_lang_String *) so;
729 if (s->count > MAXSTRINGSIZE)
732 for (i = 0; i < s->count; i++)
733 stringbuffer[i] = a->data[s->offset + i];
735 stringbuffer[i] = '\0';
741 /****************** function class_findfield_approx ****************************
743 searches in 'classinfo'-structure for a field with the
746 *******************************************************************************/
748 fieldinfo *class_findfield_approx(classinfo *c, utf *name)
751 for (i = 0; i < c->fieldscount; i++) {
752 /* compare field names */
753 if ((c->fields[i].name == name))
754 return &(c->fields[i]);
757 /* field was not found, raise exception */
758 *exceptionptr = native_new_and_init(class_java_lang_NoSuchFieldException);
763 s4 class_findfield_index_approx (classinfo *c, utf *name)
766 for (i = 0; i < c->fieldscount; i++) {
767 /* compare field names */
768 if ((c->fields[i].name == name))
772 /* field was not found, raise exception */
773 *exceptionptr = native_new_and_init(class_java_lang_NoSuchFieldException);
778 /********************** function: native_new_and_init *************************
780 Creates a new object on the heap and calls the initializer.
781 Returns the object pointer or NULL if memory is exhausted.
783 *******************************************************************************/
785 java_objectheader *native_new_and_init(classinfo *c)
788 java_objectheader *o;
790 /* if c==NULL it is probebly because loader_load failed */
791 if (!c) return *exceptionptr;
793 o = builtin_new(c); /* create object */
796 printf("native_new_and_init ");
797 utf_display(c->name);
801 /* printf("o!=NULL\n"); */
802 /* find initializer */
804 m = class_findmethod(c,
805 utf_new_char("<init>"),
806 utf_new_char("()V"));
808 if (!m) { /* initializer not found */
810 char logtext[MAXLOGTEXT];
811 sprintf(logtext, "Warning: class has no instance-initializer: ");
812 utf_sprint(logtext + strlen(logtext), c->name);
818 /* call initializer */
820 asm_calljavafunction(m, o, NULL, NULL, NULL);
826 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
829 java_objectheader *o;
831 /* if c==NULL it is probebly because loader_load failed */
832 if (!c) return *exceptionptr;
834 o = builtin_new(c); /* create object */
838 /* find initializer */
840 m = class_findmethod(c,
841 utf_new_char("<init>"),
842 utf_new_char("(Ljava/lang/String;)V"));
844 if (!m) { /* initializer not found */
846 char logtext[MAXLOGTEXT];
847 sprintf(logtext, "Warning: class has no instance-initializer: ");
848 utf_sprint(logtext + strlen(logtext), c->name);
854 /* call initializer */
856 asm_calljavafunction(m, o, s, NULL, NULL);
862 java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
865 java_objectheader *o;
867 /* if c == NULL it is probebly because loader_load failed */
868 if (!c) return *exceptionptr;
870 o = builtin_new(c); /* create object */
874 /* find initializer */
876 m = class_findmethod(c,
877 utf_new_char("<init>"),
878 utf_new_char("(I)V"));
880 if (!m) { /* initializer not found */
882 char logtext[MAXLOGTEXT];
883 sprintf(logtext, "Warning: class has no instance-initializer: ");
884 utf_sprint(logtext + strlen(logtext), c->name);
890 /* call initializer */
892 asm_calljavafunction(m, o, i, NULL, NULL);
898 /******************** function: stringtable_update ****************************
900 traverses the javastring hashtable and sets the vftbl-entries of
901 javastrings which were temporarily set to NULL, because
902 java.lang.Object was not yet loaded
904 *******************************************************************************/
906 void stringtable_update ()
908 java_lang_String *js;
910 literalstring *s; /* hashtable entry */
913 for (i = 0; i < string_hash.size; i++) {
914 s = string_hash.ptr[i];
918 js = (java_lang_String *) s->string;
920 if (!js || !js->value)
921 /* error in hashtable found */
922 panic("invalid literalstring in hashtable");
926 if (!js->header.vftbl)
927 /* vftbl of javastring is NULL */
928 js->header.vftbl = class_java_lang_String -> vftbl;
930 if (!a->header.objheader.vftbl)
931 /* vftbl of character-array is NULL */
932 a->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
934 /* follow link in external hash chain */
942 /************************* function: u2_utflength ***************************
944 returns the utf length in bytes of a u2 array
946 *****************************************************************************/
948 u4 u2_utflength(u2 *text, u4 u2_length)
950 u4 result_len = 0; /* utf length in bytes */
951 u2 ch; /* current unicode character */
954 for (len = 0; len < u2_length; len++) {
955 /* next unicode character */
958 /* determine bytes required to store unicode character as utf */
959 if (ch && (ch < 0x80))
971 /********************* function: utf_new_u2 ***********************************
973 make utf symbol from u2 array,
974 if isclassname is true '.' is replaced by '/'
976 *******************************************************************************/
978 utf *utf_new_u2(u2 *unicode_pos, u4 unicode_length, bool isclassname)
980 char *buffer; /* memory buffer for unicode characters */
981 char *pos; /* pointer to current position in buffer */
982 u4 left; /* unicode characters left */
983 u4 buflength; /* utf length in bytes of the u2 array */
984 utf *result; /* resulting utf-string */
987 /* determine utf length in bytes and allocate memory */
988 /* printf("utf_new_u2: unicode_length=%d\n",unicode_length); */
989 buflength = u2_utflength(unicode_pos, unicode_length);
990 buffer = MNEW(char, buflength);
992 /* memory allocation failed */
994 printf("length: %d\n",buflength);
995 log_text("utf_new_u2:buffer==NULL");
1001 for (i = 0; i++ < unicode_length; unicode_pos++) {
1002 /* next unicode character */
1003 u2 c = *unicode_pos;
1005 if ((c != 0) && (c < 0x80)) {
1008 if ((int) left < 0) break;
1009 /* convert classname */
1010 if (isclassname && c == '.')
1015 } else if (c < 0x800) {
1017 unsigned char high = c >> 6;
1018 unsigned char low = c & 0x3F;
1020 if ((int) left < 0) break;
1021 *pos++ = high | 0xC0;
1022 *pos++ = low | 0x80;
1026 char low = c & 0x3f;
1027 char mid = (c >> 6) & 0x3F;
1028 char high = c >> 12;
1030 if ((int) left < 0) break;
1031 *pos++ = high | 0xE0;
1032 *pos++ = mid | 0x80;
1033 *pos++ = low | 0x80;
1037 /* insert utf-string into symbol-table */
1038 result = utf_new(buffer,buflength);
1040 MFREE(buffer, char, buflength);
1046 /********************* function: javastring_toutf *****************************
1048 make utf symbol from javastring
1050 *******************************************************************************/
1052 utf *javastring_toutf(java_lang_String *string, bool isclassname)
1054 java_lang_String *str = (java_lang_String *) string;
1056 /* printf("javastring_toutf offset: %d, len %d\n",str->offset, str->count); */
1057 /* fflush(stdout); */
1059 return utf_new_u2(str->value->data + str->offset, str->count, isclassname);
1063 /********************* function: literalstring_u2 *****************************
1065 searches for the javastring with the specified u2-array in
1066 the string hashtable, if there is no such string a new one is
1069 if copymode is true a copy of the u2-array is made
1071 *******************************************************************************/
1073 java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset,
1076 literalstring *s; /* hashtable element */
1077 java_lang_String *js; /* u2-array wrapped in javastring */
1078 java_chararray *stringdata; /* copy of u2-array */
1083 //#define DEBUG_LITERALSTRING_U2
1084 #ifdef DEBUG_LITERALSTRING_U2
1085 printf("literalstring_u2: length=%d, offset=%d\n", length, offset);
1089 /* find location in hashtable */
1090 key = unicode_hashkey(a->data + offset, length);
1091 slot = key & (string_hash.size - 1);
1092 s = string_hash.ptr[slot];
1095 js = (java_lang_String *) s->string;
1097 if (length == js->count) {
1099 for (i = 0; i < length; i++) {
1100 if (a->data[offset + i] != js->value->data[i])
1104 /* string already in hashtable, free memory */
1106 lit_mem_free(a, sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10);
1108 #ifdef DEBUG_LITERALSTRING_U2
1109 printf("literalstring_u2: foundentry at %p\n", js);
1110 utf_display(javastring_toutf(js, 0));
1114 return (java_objectheader *) js;
1118 /* follow link in external hash chain */
1123 /* create copy of u2-array for new javastring */
1124 u4 arraysize = sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10;
1125 stringdata = lit_mem_alloc(arraysize);
1126 /* memcpy(stringdata, a, arraysize); */
1127 memcpy(&(stringdata->header), &(a->header), sizeof(java_arrayheader));
1128 memcpy(&(stringdata->data), &(a->data) + offset, sizeof(u2) * (length - 1) + 10);
1134 /* location in hashtable found, complete arrayheader */
1135 stringdata->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
1136 stringdata->header.size = length;
1138 /* create new javastring */
1139 js = LNEW(java_lang_String);
1140 js->header.vftbl = class_java_lang_String->vftbl;
1141 js->value = stringdata;
1145 /* create new literalstring */
1146 s = NEW(literalstring);
1147 s->hashlink = string_hash.ptr[slot];
1148 s->string = (java_objectheader *) js;
1149 string_hash.ptr[slot] = s;
1151 /* update numbe of hashtable entries */
1152 string_hash.entries++;
1154 /* reorganization of hashtable */
1155 if (string_hash.entries > (string_hash.size * 2)) {
1156 /* reorganization of hashtable, average length of
1157 the external chains is approx. 2 */
1161 hashtable newhash; /* the new hashtable */
1163 /* create new hashtable, double the size */
1164 init_hashtable(&newhash, string_hash.size * 2);
1165 newhash.entries = string_hash.entries;
1167 /* transfer elements to new hashtable */
1168 for (i = 0; i < string_hash.size; i++) {
1169 s = string_hash.ptr[i];
1171 literalstring *nexts = s->hashlink;
1172 js = (java_lang_String *) s->string;
1173 slot = unicode_hashkey(js->value->data, js->count) & (newhash.size - 1);
1175 s->hashlink = newhash.ptr[slot];
1176 newhash.ptr[slot] = s;
1178 /* follow link in external hash chain */
1183 /* dispose old table */
1184 MFREE(string_hash.ptr, void*, string_hash.size);
1185 string_hash = newhash;
1188 #ifdef DEBUG_LITERALSTRING_U2
1189 printf("literalstring_u2: newly created at %p\n", js);
1190 utf_display(javastring_toutf(js, 0));
1195 return (java_objectheader *) js;
1199 /******************** Function: literalstring_new *****************************
1201 creates a new javastring with the text of the utf-symbol
1202 and inserts it into the string hashtable
1204 *******************************************************************************/
1206 java_objectheader *literalstring_new(utf *u)
1208 char *utf_ptr = u->text; /* pointer to current unicode character in utf string */
1209 u4 utflength = utf_strlen(u); /* length of utf-string if uncompressed */
1210 java_chararray *a; /* u2-array constructed from utf string */
1212 /* log_text("literalstring_new"); */
1213 /* utf_display(u);*/
1214 /*if (utflength==0) while (1) sleep(60);*/
1215 /* log_text("------------------"); */
1216 /* allocate memory */
1217 a = lit_mem_alloc(sizeof(java_chararray) + sizeof(u2) * (utflength - 1) + 10);
1219 /* convert utf-string to u2-array */
1220 for (i = 0; i < utflength; i++)
1221 a->data[i] = utf_nextu2(&utf_ptr);
1223 return literalstring_u2(a, utflength, 0, false);
1227 /********************** function: literalstring_free **************************
1229 removes a javastring from memory
1231 ******************************************************************************/
1233 void literalstring_free(java_objectheader* sobj)
1235 java_lang_String *s = (java_lang_String *) sobj;
1236 java_chararray *a = s->value;
1238 log_text("literalstring_free called");
1240 /* dispose memory of java.lang.String object */
1241 LFREE(s, java_lang_String);
1243 /* dispose memory of java-characterarray */
1244 LFREE(a, sizeof(java_chararray) + sizeof(u2) * (a->header.size - 1)); /* +10 ?? */
1248 void copy_vftbl(vftbl **dest, vftbl *src)
1252 /* XXX this kind of copying does not work (in the general
1253 * case). The interface tables would have to be copied, too. I
1254 * don't see why we should make a copy anyway. -Edwin
1256 *dest = mem_alloc(sizeof(vftbl) + sizeof(methodptr)*(src->vftbllength-1));
1257 memcpy(*dest, src, sizeof(vftbl) - sizeof(methodptr));
1258 memcpy(&(*dest)->table, &src->table, src->vftbllength * sizeof(methodptr));
1263 /******************************************************************************************
1265 creates method signature (excluding return type) from array of
1266 class-objects representing the parameters of the method
1268 *******************************************************************************************/
1271 utf *create_methodsig(java_objectarray* types, char *retType)
1273 char *buffer; /* buffer for building the desciptor */
1274 char *pos; /* current position in buffer */
1275 utf *result; /* the method signature */
1276 u4 buffer_size = 3; /* minimal size=3: room for parenthesis and returntype */
1279 if (!types) return NULL;
1281 /* determine required buffer-size */
1282 for (i = 0; i < types->header.size; i++) {
1283 classinfo *c = (classinfo *) types->data[i];
1284 buffer_size = buffer_size + c->name->blength + 2;
1287 if (retType) buffer_size += strlen(retType);
1289 /* allocate buffer */
1290 buffer = MNEW(u1, buffer_size);
1293 /* method-desciptor starts with parenthesis */
1296 for (i = 0; i < types->header.size; i++) {
1299 /* current argument */
1300 classinfo *c = (classinfo *) types->data[i];
1302 /* current position in utf-text */
1303 char *utf_ptr = c->name->text;
1305 /* determine type of argument */
1306 if ((ch = utf_nextu2(&utf_ptr)) == '[') {
1308 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1309 *pos++ = *utf_ptr; /* copy text */
1313 /* check for primitive types */
1314 for (j = 0; j < PRIMITIVETYPE_COUNT; j++) {
1315 char *utf_pos = utf_ptr - 1;
1316 char *primitive = primitivetype_table[j].wrapname;
1319 while (utf_pos < utf_end(c->name)) {
1320 if (*utf_pos++ != *primitive++) goto nomatch;
1323 /* primitive type found */
1324 *pos++ = primitivetype_table[j].typesig;
1330 /* no primitive type and no arrayclass, so must be object */
1334 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1347 for (i = 0; i < strlen(retType); i++) {
1348 *pos++ = retType[i];
1352 /* create utf-string */
1353 result = utf_new(buffer, (pos - buffer));
1354 MFREE(buffer, u1, buffer_size);
1360 /******************************************************************************************
1362 retrieve the next argument or returntype from a descriptor
1363 and return the corresponding class
1365 *******************************************************************************************/
1367 classinfo *get_type(char **utf_ptr,char *desc_end, bool skip)
1369 classinfo *c = class_from_descriptor(*utf_ptr,desc_end,utf_ptr,
1370 (skip) ? CLASSLOAD_SKIP : CLASSLOAD_LOAD);
1373 panic("illegal descriptor");
1375 if (skip) return NULL;
1377 use_class_as_object(c);
1382 /******************************************************************************************
1384 use the descriptor of a method to generate a java/lang/Class array
1385 which contains the classes of the parametertypes of the method
1387 *******************************************************************************************/
1389 java_objectarray* get_parametertypes(methodinfo *m)
1391 utf *descr = m->descriptor; /* method-descriptor */
1392 char *utf_ptr = descr->text; /* current position in utf-text */
1393 char *desc_end = utf_end(descr); /* points behind utf string */
1394 java_objectarray* result;
1395 int parametercount = 0;
1399 utf_nextu2(&utf_ptr);
1401 /* determine number of parameters */
1402 while ( *utf_ptr != ')' ) {
1403 get_type(&utf_ptr,desc_end,true);
1407 /* create class-array */
1408 result = builtin_anewarray(parametercount, class_java_lang_Class);
1410 utf_ptr = descr->text;
1411 utf_nextu2(&utf_ptr);
1413 /* get returntype classes */
1414 for (i = 0; i < parametercount; i++)
1415 result->data[i] = (java_objectheader *) get_type(&utf_ptr,desc_end, false);
1424 /******************************************************************************************
1426 get the exceptions which can be thrown by a method
1428 *******************************************************************************************/
1430 java_objectarray* get_exceptiontypes(methodinfo *m)
1438 /******************************************************************************************
1440 get the returntype class of a method
1442 *******************************************************************************************/
1444 classinfo *get_returntype(methodinfo *m)
1446 char *utf_ptr; /* current position in utf-text */
1447 char *desc_end; /* points behind utf string */
1448 utf *desc = m->descriptor; /* method-descriptor */
1450 utf_ptr = desc->text;
1451 desc_end = utf_end(desc);
1453 /* ignore parametertypes */
1454 while ((utf_ptr<desc_end) && utf_nextu2(&utf_ptr)!=')')
1457 return get_type(&utf_ptr,desc_end, false);
1461 /*****************************************************************************/
1462 /*****************************************************************************/
1465 /*--------------------------------------------------------*/
1466 void printNativeCall(nativeCall nc) {
1469 printf("\n%s's Native Methods call:\n",nc.classname); fflush(stdout);
1470 for (i=0; i<nc.methCnt; i++) {
1471 printf("\tMethod=%s %s\n",nc.methods[i].methodname, nc.methods[i].descriptor);fflush(stdout);
1473 for (j=0; j<nc.callCnt[i]; j++) {
1474 printf("\t\t<%i,%i>aCalled = %s %s %s\n",i,j,
1475 nc.methods[i].methodCalls[j].classname,
1476 nc.methods[i].methodCalls[j].methodname,
1477 nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1480 printf("-+++++--------------------\n");fflush(stdout);
1483 /*--------------------------------------------------------*/
1484 void printCompNativeCall(nativeCompCall nc) {
1486 printf("printCompNativeCall BEGIN\n");fflush(stdout);
1487 printf("\n%s's Native Comp Methods call:\n",nc.classname->text);fflush(stdout);
1488 utf_display(nc.classname); fflush(stdout);
1490 for (i=0; i<nc.methCnt; i++) {
1491 printf("\tMethod=%s %s\n",nc.methods[i].methodname->text,nc.methods[i].descriptor->text);fflush(stdout);
1492 utf_display(nc.methods[i].methodname); fflush(stdout);
1493 utf_display(nc.methods[i].descriptor);fflush(stdout);
1494 printf("\n");fflush(stdout);
1496 for (j=0; j<nc.callCnt[i]; j++) {
1497 printf("\t\t<%i,%i>bCalled = ",i,j);fflush(stdout);
1498 utf_display(nc.methods[i].methodCalls[j].classname);fflush(stdout);
1499 utf_display(nc.methods[i].methodCalls[j].methodname); fflush(stdout);
1500 utf_display(nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1501 printf("\n");fflush(stdout);
1504 printf("---------------------\n");fflush(stdout);
1508 /*--------------------------------------------------------*/
1509 classMeth findNativeMethodCalls(utf *c, utf *m, utf *d )
1522 /*--------------------------------------------------------*/
1523 nativeCall* findNativeClassCalls(char *aclassname ) {
1526 for (i=0;i<NATIVECALLSSIZE; i++) {
1527 /* convert table to utf later to speed up search */
1528 if (strcmp(nativeCalls[i].classname, aclassname) == 0)
1529 return &nativeCalls[i];
1534 /*--------------------------------------------------------*/
1535 /*--------------------------------------------------------*/
1536 void utfNativeCall(nativeCall nc, nativeCompCall *ncc) {
1540 ncc->classname = utf_new_char(nc.classname);
1541 ncc->methCnt = nc.methCnt;
1543 for (i=0; i<nc.methCnt; i++) {
1544 ncc->methods[i].methodname = utf_new_char(nc.methods[i].methodname);
1545 ncc->methods[i].descriptor = utf_new_char(nc.methods[i].descriptor);
1546 ncc->callCnt[i] = nc.callCnt[i];
1548 for (j=0; j<nc.callCnt[i]; j++) {
1550 ncc->methods[i].methodCalls[j].classname = utf_new_char(nc.methods[i].methodCalls[j].classname);
1552 if (strcmp("", nc.methods[i].methodCalls[j].methodname) != 0) {
1553 ncc->methods[i].methodCalls[j].methodname = utf_new_char(nc.methods[i].methodCalls[j].methodname);
1554 ncc->methods[i].methodCalls[j].descriptor = utf_new_char(nc.methods[i].methodCalls[j].descriptor);
1557 ncc->methods[i].methodCalls[j].methodname = NULL;
1558 ncc->methods[i].methodCalls[j].descriptor = NULL;
1566 /*--------------------------------------------------------*/
1568 bool natcall2utf(bool natcallcompdone) {
1571 if (natcallcompdone)
1574 for (i=0;i<NATIVECALLSSIZE; i++) {
1575 utfNativeCall (nativeCalls[i], &nativeCompCalls[i]);
1581 /*--------------------------------------------------------*/
1584 java_objectarray *builtin_asm_createclasscontextarray(classinfo **end,classinfo **start) {
1585 #warning platform dependend
1586 java_objectarray *tmpArray;
1588 classinfo **current;
1590 size_t size=(((size_t)start)-((size_t)end)) / sizeof (classinfo*);
1591 printf("end %p, start %p, size %ld\n",end,start,size);
1592 if (!class_java_lang_Class)
1593 class_java_lang_Class = class_new(utf_new_char ("java/lang/Class"));
1594 if (!class_java_lang_SecurityManager)
1595 class_java_lang_SecurityManager = class_new(utf_new_char ("java/lang/SecurityManager"));
1597 if (start==class_java_lang_SecurityManager) {
1602 tmpArray=builtin_newarray(size, class_array_of(class_java_lang_Class)->vftbl);
1604 for(i=0,current=start;i<size;i++,current--) {
1607 utf_display(c->name);
1608 use_class_as_object(c);
1609 tmpArray->data[i]=c;
1615 java_lang_ClassLoader *builtin_asm_getclassloader(classinfo **end,classinfo **start) {
1616 #warning platform dependend
1618 classinfo **current;
1620 classinfo *privilegedAction;
1621 size_t size=(((size_t)start)-((size_t)end)) / sizeof (classinfo*);
1622 log_text("builtin_asm_getclassloader");
1623 printf("end %p, start %p, size %ld\n",end,start,size);
1625 if (!class_java_lang_SecurityManager)
1626 class_java_lang_SecurityManager = class_new(utf_new_char ("java/lang/SecurityManager"));
1628 if (start==class_java_lang_SecurityManager) {
1634 privilegedAction=class_new(utf_new_char("java/security/PrivilegedAction"));
1636 for(i=0,current=start;i<size;i++,current--) {
1638 if (c==privilegedAction) return NULL;
1639 if (c->classloader) return c->classloader;
1647 log_text("Java_java_lang_VMSecurityManager_currentClassLoader");
1648 init_systemclassloader();
1650 return SystemClassLoader;*/
1654 * These are local overrides for various environment variables in Emacs.
1655 * Please do not remove this and leave it at the end of the file, where
1656 * Emacs will automagically detect them.
1657 * ---------------------------------------------------------------------
1660 * indent-tabs-mode: t