- added: throw_exception, throw_exception_exit, throw_main_exception and
[cacao.git] / src / native / native.c
1 /* native.c - table of native functions
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Reinhard Grafl
28             Roman Obermaisser
29             Andreas Krall
30
31    The .hh files created with the header file generator are all
32    included here as are the C functions implementing these methods.
33
34    $Id: native.c 1190 2004-06-19 12:44:12Z twisti $
35
36 */
37
38
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <time.h>
42 #include <math.h>
43 #include <string.h>
44 #include <assert.h>
45 #include <sys/time.h>
46 #include <utime.h>
47
48 #include "config.h"
49 #include "global.h"
50 #include "main.h"
51 #include "jni.h"
52 #include "native.h"
53 #include "nativetypes.hh"
54 #include "builtin.h"
55 #include "asmpart.h"
56 #include "tables.h"
57 #include "loader.h"
58 #include "jni.h"
59 #include "jit/jit.h"
60 #include "toolbox/logging.h"
61 #include "toolbox/memory.h"
62 #include "threads/thread.h"
63 #include "threads/threadio.h"
64 #include "threads/locks.h"
65 #include "nat/java_lang_VMClass.h"
66 #include "nat/java_lang_Throwable.h"
67
68 /* Include files for IO functions */
69
70 #include <fcntl.h>
71 #include <dirent.h>
72 #include <sys/types.h>
73 #ifdef _OSF_SOURCE 
74 #include <sys/mode.h>
75 #endif
76 #include <sys/stat.h>
77
78 #include "threads/threadio.h"
79
80 /* searchpath for classfiles */
81 char *classpath;
82
83 /* for java-string to char conversion */
84 #define MAXSTRINGSIZE 1000                          
85
86
87 /******************** systemclasses required for native methods ***************/
88
89 classinfo *class_java_lang_Class;
90 classinfo *class_java_lang_VMClass;
91 classinfo *class_java_lang_System;
92 classinfo *class_java_lang_ClassLoader;
93 classinfo *class_gnu_java_lang_SystemClassLoader;
94 classinfo *class_java_lang_SecurityManager;
95 classinfo *class_java_lang_Double;
96 classinfo *class_java_lang_Float;
97 classinfo *class_java_lang_Long;
98 classinfo *class_java_lang_Byte;
99 classinfo *class_java_lang_Short;
100 classinfo *class_java_lang_Boolean;
101 classinfo *class_java_lang_Void;
102 classinfo *class_java_lang_Character;
103 classinfo *class_java_lang_Integer;
104
105 methodinfo *method_vmclass_init;
106
107
108 /* system exception classes required while compiling */
109
110 classinfo *class_java_lang_Throwable;
111
112
113 /* exception/error super class */
114
115 char *string_java_lang_Throwable =
116     "java/lang/Throwable";
117
118 char *string_java_lang_VMThrowable =
119     "java/lang/VMThrowable";
120
121
122 /* specify some exception strings for code generation */
123
124 char *string_java_lang_ArithmeticException =
125     "java/lang/ArithmeticException";
126
127 char *string_java_lang_ArithmeticException_message =
128     "/ by zero";
129
130 char *string_java_lang_ArrayIndexOutOfBoundsException =
131     "java/lang/ArrayIndexOutOfBoundsException";
132
133 char *string_java_lang_ArrayStoreException =
134     "java/lang/ArrayStoreException";
135
136 char *string_java_lang_ClassCastException =
137     "java/lang/ClassCastException";
138
139 char *string_java_lang_ClassNotFoundException =
140         "java/lang/ClassNotFoundException";
141
142 char *string_java_lang_CloneNotSupportedException =
143     "java/lang/CloneNotSupportedException";
144
145 char *string_java_lang_Exception =
146     "java/lang/Exception";
147
148 char *string_java_lang_IllegalArgumentException =
149     "java/lang/IllegalArgumentException";
150
151 char *string_java_lang_IllegalMonitorStateException =
152     "java/lang/IllegalMonitorStateException";
153
154 char *string_java_lang_NegativeArraySizeException =
155     "java/lang/NegativeArraySizeException";
156
157 char *string_java_lang_NoSuchFieldException =
158         "java/lang/NoSuchFieldException";
159
160 char *string_java_lang_NoSuchMethodException =
161         "java/lang/NoSuchMethodException";
162
163 char *string_java_lang_NullPointerException =
164     "java/lang/NullPointerException";
165
166
167 /* specify some error strings for code generation */
168
169 char *string_java_lang_AbstractMethodError =
170     "java/lang/AbstractMethodError";
171
172 char *string_java_lang_ClassCircularityError =
173     "java/lang/ClassCircularityError";
174
175 char *string_java_lang_ClassFormatError =
176     "java/lang/ClassFormatError";
177
178 char *string_java_lang_Error =
179     "java/lang/Error";
180
181 char *string_java_lang_ExceptionInInitializerError =
182     "java/lang/ExceptionInInitializerError";
183
184 char *string_java_lang_IncompatibleClassChangeError =
185     "java/lang/IncompatibleClassChangeError";
186
187 char *string_java_lang_InternalError =
188     "java/lang/InternalError";
189
190 char *string_java_lang_LinkageError =
191     "java/lang/LinkageError";
192
193 char *string_java_lang_NoClassDefFoundError =
194     "java/lang/NoClassDefFoundError";
195
196 char *string_java_lang_NoSuchFieldError =
197         "java/lang/NoSuchFieldError";
198
199 char *string_java_lang_NoSuchMethodError =
200         "java/lang/NoSuchMethodError";
201
202 char *string_java_lang_OutOfMemoryError =
203     "java/lang/OutOfMemoryError";
204
205 char *string_java_lang_VerifyError =
206     "java/lang/VerifyError";
207
208 char *string_java_lang_VirtualMachineError =
209     "java/lang/VirtualMachineError";
210
211
212 /* the system classloader object */
213 struct java_lang_ClassLoader *SystemClassLoader = NULL;
214
215 /* for raising exceptions from native methods */
216 #if !defined(USE_THREADS) || !defined(NATIVE_THREADS)
217 java_objectheader* _exceptionptr = NULL;
218 #endif
219
220 /************* use classinfo structure as java.lang.Class object **************/
221
222 void use_class_as_object(classinfo *c) 
223 {
224         if (!c->classvftbl) {
225                 c->classvftbl = true;
226
227                 c->header.vftbl = class_java_lang_Class->vftbl;
228         }
229              
230 }
231
232
233 /*************************** include native methods ***************************/ 
234
235 #ifdef USE_GTK 
236 #include "nat/GdkGraphics.c"
237 #include "nat/GtkComponentPeer.c"
238 #include "nat/GdkPixbufDecoder.c"
239 #include "nat/GtkScrollPanePeer.c"
240 #include "nat/GtkFileDialogPeer.c"
241 #include "nat/GtkLabelPeer.c"
242 #endif
243
244
245 /************************** tables for methods ********************************/
246
247 #undef JOWENN_DEBUG
248 #undef JOWENN_DEBUG1
249
250 /* table for locating native methods */
251 static struct nativeref {
252         char *classname;
253         char *methodname;
254         char *descriptor;
255         bool isstatic;
256         functionptr func;
257 } nativetable [] = {
258
259 #include "nativetable.hh"
260
261 };
262
263
264 #define NATIVETABLESIZE  (sizeof(nativetable)/sizeof(struct nativeref))
265
266 /* table for fast string comparison */
267 static struct nativecompref {
268         utf *classname;
269         utf *methodname;
270         utf *descriptor;
271         bool isstatic;
272         functionptr func;
273 } nativecomptable [NATIVETABLESIZE];
274
275 /* string comparsion table initialized */
276 static bool nativecompdone = false;
277
278
279 /******************************************************************************/
280 /******************************************************************************/
281 #include "natcalls.h"
282
283 /* string call comparison table initialized */
284
285 /******************************************************************************/
286 /******************************************************************************/
287
288 /* init_system_exceptions *****************************************************
289
290    load, link and compile exceptions used in the system
291
292 *******************************************************************************/
293
294 void init_system_exceptions()
295 {
296         classinfo *c;
297
298         /* java/lang/Throwable */
299
300         class_java_lang_Throwable =
301                 class_new(utf_new_char(string_java_lang_Throwable));
302         class_load(class_java_lang_Throwable);
303         class_link(class_java_lang_Throwable);
304 #if 0
305         compile_all_class_methods(class_java_lang_Throwable);
306
307         /* java/lang/VMThrowable */
308
309         c = class_new(utf_new_char(string_java_lang_VMThrowable));
310         class_load(c);
311         class_link(c);
312         compile_all_class_methods(c);
313
314         /* java/lang/ClassFormatError */
315
316         c = class_new(utf_new_char(string_java_lang_ClassFormatError));
317         class_load(c);
318         class_link(c);
319         compile_all_class_methods(c);
320
321         /* java/lang/Error */
322
323         c = class_new(utf_new_char(string_java_lang_Error));
324         class_load(c);
325         class_link(c);
326         compile_all_class_methods(c);
327
328         /* java/lang/Exception */
329
330         c = class_new(utf_new_char(string_java_lang_Exception));
331         class_load(c);
332         class_link(c);
333         compile_all_class_methods(c);
334
335         /* java/lang/IncompatibleClassChangeError */
336
337         c = class_new(utf_new_char(string_java_lang_IncompatibleClassChangeError));
338         class_load(c);
339         class_link(c);
340         compile_all_class_methods(c);
341
342         /* java/lang/LinkageError */
343
344         c = class_new(utf_new_char(string_java_lang_LinkageError));
345         class_load(c);
346         class_link(c);
347         compile_all_class_methods(c);
348
349         /* java/lang/NoClassDefFoundError */
350
351         c = class_new(utf_new_char(string_java_lang_NoClassDefFoundError));
352         class_load(c);
353         class_link(c);
354         compile_all_class_methods(c);
355
356         /* java/lang/NoSuchFieldError */
357
358         c = class_new(utf_new_char(string_java_lang_NoSuchFieldError));
359         class_load(c);
360         class_link(c);
361         compile_all_class_methods(c);
362
363         /* java/lang/NoSuchMethodError */
364
365         c = class_new(utf_new_char(string_java_lang_NoSuchMethodError));
366         class_load(c);
367         class_link(c);
368         compile_all_class_methods(c);
369
370         /* java/lang/OutOfMemoryError */
371
372         c = class_new(utf_new_char(string_java_lang_OutOfMemoryError));
373         class_load(c);
374         class_link(c);
375         compile_all_class_methods(c);
376
377         /* java/lang/VerifyError */
378
379         c = class_new(utf_new_char(string_java_lang_VerifyError));
380         class_load(c);
381         class_link(c);
382         compile_all_class_methods(c);
383
384         /* java/lang/VirtualMachineError */
385
386         c = class_new(utf_new_char(string_java_lang_VirtualMachineError));
387         class_load(c);
388         class_link(c);
389         compile_all_class_methods(c);
390 #endif
391 }
392
393
394 void compile_all_class_methods(classinfo *c)
395 {
396         s4 i;
397
398         for (i = 0; i < c->methodscount; i++) {
399                 (void) jit_compile(&(c->methods[i]));
400         }
401 }
402
403
404 static void throw_exception_exit_intern(bool doexit)
405 {
406         java_objectheader *xptr;
407         classinfo *c;
408         methodinfo *pss;
409
410         if (*exceptionptr) {
411                 xptr = *exceptionptr;
412
413                 /* clear exception, because we are calling jit code again */
414                 *exceptionptr = NULL;
415
416                 c = xptr->vftbl->class;
417
418                 pss = class_resolveclassmethod(c,
419                                                                            utf_new_char("printStackTrace"),
420                                                                            utf_new_char("()V"),
421                                                                            class_java_lang_Object,
422                                                                            false);
423
424                 /* print the stacktrace */
425                 if (pss) {
426                         asm_calljavafunction(pss, xptr, NULL, NULL, NULL);
427
428                         /* this normally means, we are EXTREMLY out of memory, but may be
429                            any other exception */
430                         if (*exceptionptr) {
431                                 utf_fprint_classname(stderr, c->name);
432                                 fprintf(stderr, "\n");
433                         }
434
435                 } else {
436                         utf_fprint_classname(stderr, c->name);
437                         fprintf(stderr, ": printStackTrace()V not found!\n");
438                 }
439
440                 fflush(stderr);
441
442                 /* good bye! */
443                 if (doexit) {
444                         exit(1);
445                 }
446         }
447 }
448
449
450 void throw_exception()
451 {
452         throw_exception_exit_intern(false);
453 }
454
455
456 void throw_exception_exit()
457 {
458         throw_exception_exit_intern(true);
459 }
460
461
462 void throw_main_exception()
463 {
464         fprintf(stderr, "Exception in thread \"main\" ");
465         fflush(stderr);
466
467         throw_exception_exit_intern(false);
468 }
469
470
471 void throw_main_exception_exit()
472 {
473         fprintf(stderr, "Exception in thread \"main\" ");
474         fflush(stderr);
475
476         throw_exception_exit_intern(true);
477 }
478
479
480 void throw_cacao_exception_exit(char *exception, char *message)
481 {
482         s4 i;
483         char *tmp;
484         s4 len;
485
486         len = strlen(exception);
487         tmp = MNEW(char, len);
488         strncpy(tmp, exception, len);
489
490         /* convert to classname */
491
492         for (i = len - 1; i >= 0; i--) {
493                 if (tmp[i] == '/') tmp[i] = '.';
494         }
495
496         fprintf(stderr, "Exception in thread \"main\" %s", tmp);
497
498         MFREE(tmp, char, len);
499
500         if (strlen(message) > 0)
501                 fprintf(stderr, ": %s", message);
502
503         fprintf(stderr, "\n");
504         fflush(stderr);
505
506         /* good bye! */
507         exit(1);
508 }
509
510
511 #define CREATENEW_EXCEPTION(ex) \
512         java_objectheader *newEx; \
513         java_objectheader *oldexception=*exceptionptr;\
514         *exceptionptr=0;\
515         newEx=ex;\
516         *exceptionptr=oldexception;\
517         return newEx;
518
519 java_objectheader *new_exception(char *classname)
520 {
521         classinfo *c = class_new(utf_new_char(classname));
522
523         CREATENEW_EXCEPTION(native_new_and_init(c));
524 }
525
526 java_objectheader *new_exception_message(char *classname, char *message)
527 {
528         classinfo *c = class_new(utf_new_char(classname));
529
530         CREATENEW_EXCEPTION(native_new_and_init_string(c, javastring_new_char(message)));
531 }
532
533
534 java_objectheader *new_exception_throwable(char *classname, java_lang_Throwable *throwable)
535 {
536         classinfo *c = class_new(utf_new_char(classname));
537
538         CREATENEW_EXCEPTION(native_new_and_init_throwable(c, throwable));
539 }
540
541
542 java_objectheader *new_exception_utfmessage(char *classname, utf *message)
543 {
544         classinfo *c = class_new(utf_new_char(classname));
545
546         CREATENEW_EXCEPTION(native_new_and_init_string(c, javastring_new(message)));
547 }
548
549
550 java_objectheader *new_exception_javastring(char *classname, java_lang_String *message)
551 {
552         classinfo *c = class_new(utf_new_char(classname));
553
554         CREATENEW_EXCEPTION(native_new_and_init_string(c, message));
555 }
556
557
558 java_objectheader *new_exception_int(char *classname, s4 i)
559 {
560         classinfo *c = class_new(utf_new_char(classname));
561
562         CREATENEW_EXCEPTION(native_new_and_init_int(c, i));
563 }
564
565
566 /*********************** function: native_loadclasses **************************
567
568         load classes required for native methods        
569
570 *******************************************************************************/
571
572 void native_loadclasses()
573 {
574         static int classesLoaded = 0; /*temporary hack JoWenn*/
575
576         if (classesLoaded)
577                 return;
578
579         classesLoaded = 1;
580
581         class_java_lang_Cloneable =
582                 class_new(utf_new_char("java/lang/Cloneable"));
583         class_load(class_java_lang_Cloneable);
584         class_link(class_java_lang_Cloneable);
585
586         class_java_lang_Class =
587                 class_new(utf_new_char("java/lang/Class"));
588         class_load(class_java_lang_Class);
589         class_link(class_java_lang_Class);
590
591         class_java_lang_VMClass =
592                 class_new(utf_new_char("java/lang/VMClass"));
593         class_load(class_java_lang_VMClass);
594         class_link(class_java_lang_VMClass);
595
596         class_java_lang_ClassLoader =
597                 class_new(utf_new_char("java/lang/ClassLoader"));
598         class_load(class_java_lang_ClassLoader);
599         class_link(class_java_lang_ClassLoader);
600
601         /* load classes for wrapping primitive types */
602         class_java_lang_Double = class_new(utf_new_char("java/lang/Double"));
603         class_load(class_java_lang_Double);
604         class_link(class_java_lang_Double);
605
606         class_java_lang_Float = class_new(utf_new_char("java/lang/Float"));
607         class_load(class_java_lang_Float);
608         class_link(class_java_lang_Float);
609
610         class_java_lang_Character =     class_new(utf_new_char("java/lang/Character"));
611         class_load(class_java_lang_Character);
612         class_link(class_java_lang_Character);
613
614         class_java_lang_Integer = class_new(utf_new_char("java/lang/Integer"));
615         class_load(class_java_lang_Integer);
616         class_link(class_java_lang_Integer);
617
618         class_java_lang_Long = class_new(utf_new_char("java/lang/Long"));
619         class_load(class_java_lang_Long);
620         class_link(class_java_lang_Long);
621
622         class_java_lang_Byte = class_new(utf_new_char("java/lang/Byte"));
623         class_load(class_java_lang_Byte);
624         class_link(class_java_lang_Byte);
625
626         class_java_lang_Short = class_new(utf_new_char("java/lang/Short"));
627         class_load(class_java_lang_Short);
628         class_link(class_java_lang_Short);
629
630         class_java_lang_Boolean = class_new(utf_new_char("java/lang/Boolean"));
631         class_load(class_java_lang_Boolean);
632         class_link(class_java_lang_Boolean);
633
634         class_java_lang_Void = class_new(utf_new_char("java/lang/Void"));
635         class_load(class_java_lang_Void);
636         class_link(class_java_lang_Void);
637
638 }
639
640
641 /*****************************************************************************
642
643         create systemclassloader object and initialize instance fields  
644
645 ******************************************************************************/
646
647 void init_systemclassloader() 
648 {
649         log_text("init_systemclassloader");
650         if (!SystemClassLoader) {
651                 native_loadclasses();
652                 log_text("Initializing new system class loader");
653                 /* create object and call initializer */
654                 SystemClassLoader = (java_lang_ClassLoader *) native_new_and_init(class_new(utf_new_char("gnu/java/lang/SystemClassLoader")));
655
656                 /* systemclassloader has no parent */
657                 SystemClassLoader->parent      = NULL;
658                 SystemClassLoader->initialized = true;
659         }
660         log_text("leaving system class loader");
661 }
662
663
664 /********************* function: native_setclasspath **************************/
665  
666 void native_setclasspath(char *path)
667 {
668         /* set searchpath for classfiles */
669         classpath = path;
670 }
671
672
673 /*********************** Function: native_findfunction *************************
674
675         Looks up a method (must have the same class name, method name, descriptor
676         and 'static'ness) and returns a function pointer to it.
677         Returns: function pointer or NULL (if there is no such method)
678
679         Remark: For faster operation, the names/descriptors are converted from C
680                 strings to Unicode the first time this function is called.
681
682 *******************************************************************************/
683
684 functionptr native_findfunction(utf *cname, utf *mname, 
685                                                                 utf *desc, bool isstatic)
686 {
687         int i;
688         /* entry of table for fast string comparison */
689         struct nativecompref *n;
690         /* for warning message if no function is found */
691         char *buffer;                   
692         int buffer_len;
693
694         isstatic = isstatic ? true : false;
695         
696         if (!nativecompdone) {
697                 for (i = 0; i < NATIVETABLESIZE; i++) {
698                         nativecomptable[i].classname  = 
699                                 utf_new_char(nativetable[i].classname);
700                         nativecomptable[i].methodname = 
701                                 utf_new_char(nativetable[i].methodname);
702                         nativecomptable[i].descriptor = 
703                                 utf_new_char(nativetable[i].descriptor);
704                         nativecomptable[i].isstatic   = 
705                                 nativetable[i].isstatic;
706                         nativecomptable[i].func       = 
707                                 nativetable[i].func;
708                 }
709                 nativecompdone = true;
710         }
711
712 #ifdef JOWENN_DEBUG
713         buffer_len = 
714                 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
715         
716         buffer = MNEW(char, buffer_len);
717
718         strcpy(buffer, "searching matching function in native table:");
719         utf_sprint(buffer+strlen(buffer), mname);
720         strcpy(buffer+strlen(buffer), ": ");
721         utf_sprint(buffer+strlen(buffer), desc);
722         strcpy(buffer+strlen(buffer), " for class ");
723         utf_sprint(buffer+strlen(buffer), cname);
724
725         log_text(buffer);       
726
727         MFREE(buffer, char, buffer_len);
728 #endif
729                 
730         for (i = 0; i < NATIVETABLESIZE; i++) {
731                 n = &(nativecomptable[i]);
732
733                 if (cname == n->classname && mname == n->methodname &&
734                     desc == n->descriptor && isstatic == n->isstatic)
735                         return n->func;
736 #ifdef JOWENN_DEBUG
737                         else {
738                                 if (cname == n->classname && mname == n->methodname )  log_text("static and descriptor mismatch");
739                         
740                                 else {
741                                         buffer_len = 
742                                           utf_strlen(n->classname) + utf_strlen(n->methodname) + utf_strlen(n->descriptor) + 64;
743         
744                                         buffer = MNEW(char, buffer_len);
745
746                                         strcpy(buffer, "comparing with:");
747                                         utf_sprint(buffer+strlen(buffer), n->methodname);
748                                         strcpy (buffer+strlen(buffer), ": ");
749                                         utf_sprint(buffer+strlen(buffer), n->descriptor);
750                                         strcpy(buffer+strlen(buffer), " for class ");
751                                         utf_sprint(buffer+strlen(buffer), n->classname);
752
753                                         log_text(buffer);       
754
755                                         MFREE(buffer, char, buffer_len);
756                                 }
757                         } 
758 #endif
759         }
760
761                 
762         /* no function was found, display warning */
763
764         buffer_len = 
765                 utf_strlen(cname) + utf_strlen(mname) + utf_strlen(desc) + 64;
766
767         buffer = MNEW(char, buffer_len);
768
769         strcpy(buffer, "warning: native function ");
770         utf_sprint(buffer + strlen(buffer), mname);
771         strcpy(buffer + strlen(buffer), ": ");
772         utf_sprint(buffer + strlen(buffer), desc);
773         strcpy(buffer + strlen(buffer), " not found in class ");
774         utf_sprint(buffer + strlen(buffer), cname);
775
776         log_text(buffer);       
777
778         MFREE(buffer, char, buffer_len);
779
780 /*      exit(1); */
781
782         /* keep compiler happy */
783         return NULL;
784 }
785
786
787 /********************** function: javastring_new *******************************
788
789         creates a new object of type java/lang/String with the text of 
790         the specified utf8-string
791
792         return: pointer to the string or NULL if memory is exhausted.   
793
794 *******************************************************************************/
795
796 java_lang_String *javastring_new(utf *u)
797 {
798         char *utf_ptr = u->text;        /* current utf character in utf string    */
799         int utflength = utf_strlen(u);  /* length of utf-string if uncompressed   */
800         java_lang_String *s;                /* result-string                          */
801         java_chararray *a;
802         s4 i;
803         
804         s = (java_lang_String *) builtin_new(class_java_lang_String);
805         a = builtin_newarray_char(utflength);
806
807         /* javastring or character-array could not be created */
808         if (!a || !s)
809                 return NULL;
810
811         /* decompress utf-string */
812         for (i = 0; i < utflength; i++)
813                 a->data[i] = utf_nextu2(&utf_ptr);
814         
815         /* set fields of the javastring-object */
816         s->value  = a;
817         s->offset = 0;
818         s->count  = utflength;
819
820         return s;
821 }
822
823
824 /********************** function: javastring_new_char **************************
825
826         creates a new java/lang/String object which contains the convertet
827         C-string passed via text.
828
829         return: the object pointer or NULL if memory is exhausted.
830
831 *******************************************************************************/
832
833 java_lang_String *javastring_new_char(char *text)
834 {
835         s4 i;
836         s4 len = strlen(text); /* length of the string */
837         java_lang_String *s;   /* result-string */
838         java_chararray *a;
839         
840         s = (java_lang_String *) builtin_new(class_java_lang_String);
841         a = builtin_newarray_char(len);
842
843         /* javastring or character-array could not be created */
844         if (!a || !s)
845                 return NULL;
846
847         /* copy text */
848         for (i = 0; i < len; i++)
849                 a->data[i] = text[i];
850         
851         /* set fields of the javastring-object */
852         s->value  = a;
853         s->offset = 0;
854         s->count  = len;
855
856         return s;
857 }
858
859
860 /************************* function javastring_tochar **************************
861
862         converts a Java string into a C string.
863         
864         return: pointer to C string
865         
866         Caution: every call of this function overwrites the previous string !!!
867         
868 *******************************************************************************/
869
870 static char stringbuffer[MAXSTRINGSIZE];
871
872 char *javastring_tochar(java_objectheader *so) 
873 {
874         java_lang_String *s = (java_lang_String *) so;
875         java_chararray *a;
876         s4 i;
877         
878         if (!s)
879                 return "";
880
881         a = s->value;
882
883         if (!a)
884                 return "";
885
886         if (s->count > MAXSTRINGSIZE)
887                 return "";
888
889         for (i = 0; i < s->count; i++)
890                 stringbuffer[i] = a->data[s->offset + i];
891
892         stringbuffer[i] = '\0';
893
894         return stringbuffer;
895 }
896
897
898 /****************** function class_findfield_approx ****************************
899         
900         searches in 'classinfo'-structure for a field with the
901         specified name
902
903 *******************************************************************************/
904  
905 fieldinfo *class_findfield_approx(classinfo *c, utf *name)
906 {
907         s4 i;
908
909         for (i = 0; i < c->fieldscount; i++) {
910                 /* compare field names */
911                 if ((c->fields[i].name == name))
912                         return &(c->fields[i]);
913         }
914
915         /* field was not found, raise exception */      
916         *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
917
918         return NULL;
919 }
920
921
922 s4 class_findfield_index_approx(classinfo *c, utf *name)
923 {
924         s4 i;
925
926         for (i = 0; i < c->fieldscount; i++) {
927                 /* compare field names */
928                 if ((c->fields[i].name == name))
929                         return i;
930         }
931
932         /* field was not found, raise exception */      
933         *exceptionptr = new_exception(string_java_lang_NoSuchFieldException);
934
935         return -1;
936 }
937
938
939 /********************** function: native_new_and_init *************************
940
941         Creates a new object on the heap and calls the initializer.
942         Returns the object pointer or NULL if memory is exhausted.
943                         
944 *******************************************************************************/
945
946 java_objectheader *native_new_and_init(classinfo *c)
947 {
948         methodinfo *m;
949         java_objectheader *o;
950
951         if (!c)
952                 return *exceptionptr;
953
954         o = builtin_new(c);                 /* create object                      */
955         
956         if (!o)
957                 return NULL;
958
959         /* find initializer */
960
961         m = class_findmethod(c,
962                                                  utf_new_char("<init>"),
963                                                  utf_new_char("()V"));
964                                                       
965         if (!m) {                           /* initializer not found              */
966                 if (verbose) {
967                         char logtext[MAXLOGTEXT];
968                         sprintf(logtext, "Warning: class has no instance-initializer: ");
969                         utf_sprint_classname(logtext + strlen(logtext), c->name);
970                         log_text(logtext);
971                 }
972                 return o;
973         }
974
975         /* call initializer */
976
977         asm_calljavafunction(m, o, NULL, NULL, NULL);
978
979         return o;
980 }
981
982
983 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s)
984 {
985         methodinfo *m;
986         java_objectheader *o;
987
988         if (!c)
989                 return *exceptionptr;
990
991         o = builtin_new(c);          /* create object          */
992
993         if (!o)
994                 return NULL;
995
996         /* find initializer */
997
998         m = class_findmethod(c,
999                                                  utf_new_char("<init>"),
1000                                                  utf_new_char("(Ljava/lang/String;)V"));
1001                                                       
1002         if (!m) {                                       /* initializer not found  */
1003                 if (verbose) {
1004                         char logtext[MAXLOGTEXT];
1005                         sprintf(logtext, "Warning: class has no instance-initializer: ");
1006                         utf_sprint_classname(logtext + strlen(logtext), c->name);
1007                         log_text(logtext);
1008                 }
1009                 return o;
1010         }
1011
1012         /* call initializer */
1013
1014         asm_calljavafunction(m, o, s, NULL, NULL);
1015
1016         return o;
1017 }
1018
1019
1020 java_objectheader *native_new_and_init_int(classinfo *c, s4 i)
1021 {
1022         methodinfo *m;
1023         java_objectheader *o;
1024
1025         if (!c)
1026                 return *exceptionptr;
1027
1028         o = builtin_new(c);          /* create object          */
1029         
1030         if (!o) return NULL;
1031
1032         /* find initializer */
1033
1034         m = class_findmethod(c,
1035                                                  utf_new_char("<init>"),
1036                                                  utf_new_char("(I)V"));
1037                                                       
1038         if (!m) {                                       /* initializer not found  */
1039                 if (verbose) {
1040                         char logtext[MAXLOGTEXT];
1041                         sprintf(logtext, "Warning: class has no instance-initializer: ");
1042                         utf_sprint_classname(logtext + strlen(logtext), c->name);
1043                         log_text(logtext);
1044                 }
1045                 return o;
1046         }
1047
1048         /* call initializer */
1049
1050 #if defined(__I386__) || defined(__POWERPC__)
1051         asm_calljavafunction(m, o, (void *) i, NULL, NULL);
1052 #else
1053         asm_calljavafunction(m, o, (void *) (s8) i, NULL, NULL);
1054 #endif
1055
1056         return o;
1057 }
1058
1059
1060 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t)
1061 {
1062         methodinfo *m;
1063         java_objectheader *o;
1064
1065         if (!c)
1066                 return *exceptionptr;
1067
1068         o = builtin_new(c);          /* create object          */
1069         
1070         if (!o) return NULL;
1071
1072         /* find initializer */
1073
1074         m = class_findmethod(c,
1075                                                  utf_new_char("<init>"),
1076                                                  utf_new_char("(Ljava/lang/Throwable;)V"));
1077                                                       
1078         if (!m) {                                       /* initializer not found  */
1079                 if (verbose) {
1080                         char logtext[MAXLOGTEXT];
1081                         sprintf(logtext, "Warning: class has no instance-initializer: ");
1082                         utf_sprint_classname(logtext + strlen(logtext), c->name);
1083                         log_text(logtext);
1084                 }
1085                 return o;
1086         }
1087
1088         /* call initializer */
1089
1090         asm_calljavafunction(m, o, t, NULL, NULL);
1091
1092         return o;
1093 }
1094
1095
1096 /******************** function: stringtable_update ****************************
1097
1098         traverses the javastring hashtable and sets the vftbl-entries of
1099         javastrings which were temporarily set to NULL, because 
1100         java.lang.Object was not yet loaded
1101
1102 *******************************************************************************/
1103  
1104 void stringtable_update ()
1105 {
1106         java_lang_String *js;   
1107         java_chararray *a;
1108         literalstring *s;       /* hashtable entry */
1109         int i;
1110
1111         for (i = 0; i < string_hash.size; i++) {
1112                 s = string_hash.ptr[i];
1113                 if (s) {
1114                         while (s) {
1115                                                                 
1116                                 js = (java_lang_String *) s->string;
1117                                 
1118                                 if (!js || !js->value) 
1119                                         /* error in hashtable found */
1120                                         panic("invalid literalstring in hashtable");
1121
1122                                 a = js->value;
1123
1124                                 if (!js->header.vftbl) 
1125                                         /* vftbl of javastring is NULL */ 
1126                                         js->header.vftbl = class_java_lang_String->vftbl;
1127
1128                                 if (!a->header.objheader.vftbl) 
1129                                         /* vftbl of character-array is NULL */ 
1130                                         a->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
1131
1132                                 /* follow link in external hash chain */
1133                                 s = s->hashlink;
1134                         }       
1135                 }               
1136         }
1137 }
1138
1139
1140 /************************* function: u2_utflength ***************************
1141
1142         returns the utf length in bytes of a u2 array 
1143
1144 *****************************************************************************/
1145
1146 u4 u2_utflength(u2 *text, u4 u2_length)
1147 {
1148         u4 result_len =  0;  /* utf length in bytes  */
1149         u2 ch;               /* current unicode character */
1150         u4 len;
1151         
1152         for (len = 0; len < u2_length; len++) {
1153                 /* next unicode character */
1154                 ch = *text++;
1155           
1156                 /* determine bytes required to store unicode character as utf */
1157                 if (ch && (ch < 0x80)) 
1158                         result_len++;
1159                 else if (ch < 0x800)
1160                         result_len += 2;        
1161                 else 
1162                         result_len += 3;        
1163         }
1164
1165     return result_len;
1166 }
1167
1168
1169 /********************* function: utf_new_u2 ***********************************
1170
1171         make utf symbol from u2 array, 
1172         if isclassname is true '.' is replaced by '/'
1173
1174 *******************************************************************************/
1175
1176 utf *utf_new_u2(u2 *unicode_pos, u4 unicode_length, bool isclassname)
1177 {
1178         char *buffer; /* memory buffer for  unicode characters */
1179         char *pos;    /* pointer to current position in buffer */
1180         u4 left;      /* unicode characters left */
1181         u4 buflength; /* utf length in bytes of the u2 array  */
1182         utf *result;  /* resulting utf-string */
1183         int i;          
1184
1185         /* determine utf length in bytes and allocate memory */
1186         /* printf("utf_new_u2: unicode_length=%d\n",unicode_length);            */
1187         buflength = u2_utflength(unicode_pos, unicode_length); 
1188         buffer    = MNEW(char, buflength);
1189  
1190         left = buflength;
1191         pos  = buffer;
1192
1193         for (i = 0; i++ < unicode_length; unicode_pos++) {
1194                 /* next unicode character */
1195                 u2 c = *unicode_pos;
1196                 
1197                 if ((c != 0) && (c < 0x80)) {
1198                         /* 1 character */       
1199                         left--;
1200                 if ((int) left < 0) break;
1201                         /* convert classname */
1202                         if (isclassname && c == '.')
1203                                 *pos++ = '/';
1204                         else
1205                                 *pos++ = (char) c;
1206
1207                 } else if (c < 0x800) {             
1208                         /* 2 characters */                              
1209                 unsigned char high = c >> 6;
1210                 unsigned char low  = c & 0x3F;
1211                         left = left - 2;
1212                 if ((int) left < 0) break;
1213                 *pos++ = high | 0xC0; 
1214                 *pos++ = low  | 0x80;     
1215
1216                 } else {         
1217                 /* 3 characters */                              
1218                 char low  = c & 0x3f;
1219                 char mid  = (c >> 6) & 0x3F;
1220                 char high = c >> 12;
1221                         left = left - 3;
1222                 if ((int) left < 0) break;
1223                 *pos++ = high | 0xE0; 
1224                 *pos++ = mid  | 0x80;  
1225                 *pos++ = low  | 0x80;   
1226                 }
1227         }
1228         
1229         /* insert utf-string into symbol-table */
1230         result = utf_new(buffer,buflength);
1231
1232         MFREE(buffer, char, buflength);
1233
1234         return result;
1235 }
1236
1237
1238 /********************* function: javastring_toutf *****************************
1239
1240         make utf symbol from javastring
1241
1242 *******************************************************************************/
1243
1244 utf *javastring_toutf(java_lang_String *string, bool isclassname)
1245 {
1246         java_lang_String *str = (java_lang_String *) string;
1247
1248 /*      printf("javastring_toutf offset: %d, len %d\n",str->offset, str->count); */
1249 /*      fflush(stdout); */
1250
1251         return utf_new_u2(str->value->data + str->offset, str->count, isclassname);
1252 }
1253
1254
1255 /********************* function: literalstring_u2 *****************************
1256
1257     searches for the javastring with the specified u2-array in 
1258     the string hashtable, if there is no such string a new one is 
1259     created 
1260
1261     if copymode is true a copy of the u2-array is made
1262
1263 *******************************************************************************/
1264
1265 java_objectheader *literalstring_u2(java_chararray *a, u4 length, u4 offset,
1266                                                                         bool copymode)
1267 {
1268     literalstring *s;                /* hashtable element */
1269     java_lang_String *js;            /* u2-array wrapped in javastring */
1270     java_chararray *stringdata;      /* copy of u2-array */      
1271         classinfo *c;
1272     u4 key;
1273     u4 slot;
1274     u2 i;
1275
1276 //#define DEBUG_LITERALSTRING_U2
1277 #ifdef DEBUG_LITERALSTRING_U2
1278     printf("literalstring_u2: length=%d, offset=%d\n", length, offset);
1279         fflush(stdout);
1280 #endif
1281     
1282     /* find location in hashtable */
1283     key  = unicode_hashkey(a->data + offset, length);
1284     slot = key & (string_hash.size - 1);
1285     s    = string_hash.ptr[slot];
1286
1287     while (s) {
1288                 js = (java_lang_String *) s->string;
1289
1290                 if (length == js->count) {
1291                         /* compare text */
1292                         for (i = 0; i < length; i++) {
1293                                 if (a->data[offset + i] != js->value->data[i])
1294                                         goto nomatch;
1295                         }
1296
1297                         /* string already in hashtable, free memory */
1298                         if (!copymode)
1299                                 lit_mem_free(a, sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10);
1300
1301 #ifdef DEBUG_LITERALSTRING_U2
1302                         printf("literalstring_u2: foundentry at %p\n", js);
1303                         utf_display(javastring_toutf(js, 0));
1304                         printf("\n\n");
1305                         fflush(stdout);
1306 #endif
1307                         return (java_objectheader *) js;
1308                 }
1309
1310         nomatch:
1311                 /* follow link in external hash chain */
1312                 s = s->hashlink;
1313     }
1314
1315     if (copymode) {
1316                 /* create copy of u2-array for new javastring */
1317                 u4 arraysize = sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10;
1318                 stringdata = lit_mem_alloc(arraysize);
1319 /*              memcpy(stringdata, a, arraysize); */
1320                 memcpy(&(stringdata->header), &(a->header), sizeof(java_arrayheader));
1321                 memcpy(&(stringdata->data), &(a->data) + offset, sizeof(u2) * (length - 1) + 10);
1322
1323     } else {
1324                 stringdata = a;
1325         }
1326
1327     /* location in hashtable found, complete arrayheader */
1328     stringdata->header.objheader.vftbl = primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
1329     stringdata->header.size = length;
1330
1331         /* if we use eager loading, we have to check loaded String class */
1332         if (opt_eager) {
1333                 class_java_lang_String =
1334                         class_new_intern(utf_new_char("java/lang/String"));
1335
1336                 if (!class_load(class_java_lang_String))
1337                         return NULL;
1338
1339                 list_addfirst(&unlinkedclasses, class_java_lang_String);
1340         }
1341
1342     /* create new javastring */
1343     js = LNEW(java_lang_String);
1344         js->header.vftbl = class_java_lang_String->vftbl;
1345     js->value  = stringdata;
1346     js->offset = 0;
1347     js->count  = length;
1348
1349 #ifdef DEBUG_LITERALSTRING_U2
1350         printf("literalstring_u2: newly created at %p\n", js);
1351         utf_display(javastring_toutf(js, 0));
1352         printf("\n\n");
1353         fflush(stdout);
1354 #endif
1355                         
1356     /* create new literalstring */
1357     s = NEW(literalstring);
1358     s->hashlink = string_hash.ptr[slot];
1359     s->string   = (java_objectheader *) js;
1360     string_hash.ptr[slot] = s;
1361
1362     /* update number of hashtable entries */
1363     string_hash.entries++;
1364
1365     /* reorganization of hashtable */       
1366     if (string_hash.entries > (string_hash.size * 2)) {
1367                 /* reorganization of hashtable, average length of 
1368          the external chains is approx. 2                */  
1369
1370                 u4 i;
1371                 literalstring *s;
1372                 hashtable newhash; /* the new hashtable */
1373       
1374                 /* create new hashtable, double the size */
1375                 init_hashtable(&newhash, string_hash.size * 2);
1376                 newhash.entries = string_hash.entries;
1377       
1378                 /* transfer elements to new hashtable */
1379                 for (i = 0; i < string_hash.size; i++) {
1380                         s = string_hash.ptr[i];
1381                         while (s) {
1382                                 literalstring *nexts = s->hashlink;
1383                                 js   = (java_lang_String *) s->string;
1384                                 slot = unicode_hashkey(js->value->data, js->count) & (newhash.size - 1);
1385           
1386                                 s->hashlink = newhash.ptr[slot];
1387                                 newhash.ptr[slot] = s;
1388         
1389                                 /* follow link in external hash chain */  
1390                                 s = nexts;
1391                         }
1392                 }
1393         
1394                 /* dispose old table */ 
1395                 MFREE(string_hash.ptr, void*, string_hash.size);
1396                 string_hash = newhash;
1397     }
1398
1399     return (java_objectheader *) js;
1400 }
1401
1402
1403 /******************** Function: literalstring_new *****************************
1404
1405     creates a new javastring with the text of the utf-symbol
1406     and inserts it into the string hashtable
1407
1408 *******************************************************************************/
1409
1410 java_objectheader *literalstring_new(utf *u)
1411 {
1412     char *utf_ptr = u->text;         /* pointer to current unicode character in utf string */
1413     u4 utflength  = utf_strlen(u);   /* length of utf-string if uncompressed */
1414     java_chararray *a;               /* u2-array constructed from utf string */
1415     u4 i;
1416
1417     /* allocate memory */ 
1418     a = lit_mem_alloc(sizeof(java_chararray) + sizeof(u2) * (utflength - 1) + 10);
1419
1420     /* convert utf-string to u2-array */
1421     for (i = 0; i < utflength; i++)
1422                 a->data[i] = utf_nextu2(&utf_ptr);
1423
1424     return literalstring_u2(a, utflength, 0, false);
1425 }
1426
1427
1428 /********************** function: literalstring_free **************************
1429
1430         removes a javastring from memory                       
1431
1432 ******************************************************************************/
1433
1434 void literalstring_free(java_objectheader* sobj)
1435 {
1436         java_lang_String *s = (java_lang_String *) sobj;
1437         java_chararray *a = s->value;
1438
1439         /* dispose memory of java.lang.String object */
1440         LFREE(s, java_lang_String);
1441
1442         /* dispose memory of java-characterarray */
1443         LFREE(a, sizeof(java_chararray) + sizeof(u2) * (a->header.size - 1)); /* +10 ?? */
1444 }
1445
1446
1447 void copy_vftbl(vftbl **dest, vftbl *src)
1448 {
1449     *dest = src;
1450 #if 0
1451     /* XXX this kind of copying does not work (in the general
1452      * case). The interface tables would have to be copied, too. I
1453      * don't see why we should make a copy anyway. -Edwin
1454      */
1455         *dest = mem_alloc(sizeof(vftbl) + sizeof(methodptr)*(src->vftbllength-1));
1456         memcpy(*dest, src, sizeof(vftbl) - sizeof(methodptr));
1457         memcpy(&(*dest)->table, &src->table, src->vftbllength * sizeof(methodptr));
1458 #endif
1459 }
1460
1461
1462 /******************************************************************************************                                                                                                             
1463
1464         creates method signature (excluding return type) from array of 
1465         class-objects representing the parameters of the method 
1466
1467 *******************************************************************************************/
1468
1469
1470 utf *create_methodsig(java_objectarray* types, char *retType)
1471 {
1472     char *buffer;       /* buffer for building the desciptor */
1473     char *pos;          /* current position in buffer */
1474     utf *result;        /* the method signature */
1475     u4 buffer_size = 3; /* minimal size=3: room for parenthesis and returntype */
1476     u4 i, j;
1477  
1478     if (!types) return NULL;
1479
1480     /* determine required buffer-size */    
1481     for (i = 0; i < types->header.size; i++) {
1482                 classinfo *c = (classinfo *) types->data[i];
1483                 buffer_size  = buffer_size + c->name->blength + 2;
1484     }
1485
1486     if (retType) buffer_size += strlen(retType);
1487
1488     /* allocate buffer */
1489     buffer = MNEW(u1, buffer_size);
1490     pos    = buffer;
1491     
1492     /* method-desciptor starts with parenthesis */
1493     *pos++ = '(';
1494
1495     for (i = 0; i < types->header.size; i++) {
1496                 char ch;           
1497
1498                 /* current argument */
1499             classinfo *c = (classinfo *) types->data[i];
1500
1501             /* current position in utf-text */
1502             char *utf_ptr = c->name->text; 
1503             
1504             /* determine type of argument */
1505             if ((ch = utf_nextu2(&utf_ptr)) == '[') {
1506                 /* arrayclass */
1507                 for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1508                                 *pos++ = *utf_ptr; /* copy text */
1509                         }
1510
1511             } else {            
1512                         /* check for primitive types */
1513                         for (j = 0; j < PRIMITIVETYPE_COUNT; j++) {
1514                                 char *utf_pos   = utf_ptr - 1;
1515                                 char *primitive = primitivetype_table[j].wrapname;
1516
1517                                 /* compare text */
1518                                 while (utf_pos < utf_end(c->name)) {
1519                                         if (*utf_pos++ != *primitive++) goto nomatch;
1520                                 }
1521
1522                                 /* primitive type found */
1523                                 *pos++ = primitivetype_table[j].typesig;
1524                                 goto next_type;
1525
1526                         nomatch:
1527                                 ;
1528                         }
1529
1530                         /* no primitive type and no arrayclass, so must be object */
1531                         *pos++ = 'L';
1532
1533                         /* copy text */
1534                         for (utf_ptr--; utf_ptr < utf_end(c->name); utf_ptr++) {
1535                                 *pos++ = *utf_ptr;
1536                         }
1537
1538                         *pos++ = ';';
1539
1540                 next_type:
1541                         ;
1542                 }  
1543     }       
1544
1545     *pos++ = ')';
1546
1547     if (retType) {
1548                 for (i = 0; i < strlen(retType); i++) {
1549                         *pos++ = retType[i];
1550                 }
1551     }
1552
1553     /* create utf-string */
1554     result = utf_new(buffer, (pos - buffer));
1555     MFREE(buffer, u1, buffer_size);
1556
1557     return result;
1558 }
1559
1560
1561 /******************************************************************************************
1562
1563         retrieve the next argument or returntype from a descriptor
1564         and return the corresponding class 
1565
1566 *******************************************************************************************/
1567
1568 classinfo *get_type(char **utf_ptr,char *desc_end, bool skip)
1569 {
1570     classinfo *c = class_from_descriptor(*utf_ptr,desc_end,utf_ptr,
1571                                          (skip) ? CLASSLOAD_SKIP : CLASSLOAD_LOAD);
1572     if (!c)
1573         /* unknown type */
1574         panic("illegal descriptor");
1575
1576     if (skip) return NULL;
1577
1578     use_class_as_object(c);
1579     return c;
1580 }
1581
1582
1583 /******************************************************************************************
1584
1585         use the descriptor of a method to generate a java/lang/Class array
1586         which contains the classes of the parametertypes of the method
1587
1588 *******************************************************************************************/
1589
1590 java_objectarray* get_parametertypes(methodinfo *m) 
1591 {
1592     utf  *descr    =  m->descriptor;    /* method-descriptor */ 
1593     char *utf_ptr  =  descr->text;      /* current position in utf-text */
1594     char *desc_end =  utf_end(descr);   /* points behind utf string     */
1595     java_objectarray* result;
1596     int parametercount = 0;
1597     int i;
1598
1599     /* skip '(' */
1600     utf_nextu2(&utf_ptr);
1601   
1602     /* determine number of parameters */
1603     while ( *utf_ptr != ')' ) {
1604         get_type(&utf_ptr,desc_end,true);
1605         parametercount++;
1606     }
1607
1608     /* create class-array */
1609     result = builtin_anewarray(parametercount, class_java_lang_Class);
1610
1611     utf_ptr  =  descr->text;
1612     utf_nextu2(&utf_ptr);
1613
1614     /* get returntype classes */
1615     for (i = 0; i < parametercount; i++)
1616             result->data[i] = (java_objectheader *) get_type(&utf_ptr,desc_end, false);
1617
1618     return result;
1619 }
1620
1621
1622
1623
1624
1625 /******************************************************************************************
1626
1627         get the exceptions which can be thrown by a method      
1628
1629 *******************************************************************************************/
1630
1631 java_objectarray* get_exceptiontypes(methodinfo *m) {
1632     u2 exccount=m->thrownexceptionscount;
1633     u2 i;
1634     java_objectarray *result;
1635     /* create class-array */
1636     result = builtin_anewarray(exccount, class_java_lang_Class);
1637     for (i=0;i<exccount;i++) {
1638         java_objectheader *oh=(java_objectheader*)(m->thrownexceptions[i]);
1639         use_class_as_object(oh);
1640         result->data[i]=oh;
1641     }
1642     return result;
1643 }
1644
1645
1646
1647
1648
1649 /******************************************************************************************
1650
1651         get the returntype class of a method
1652
1653 *******************************************************************************************/
1654
1655 classinfo *get_returntype(methodinfo *m) 
1656 {
1657         char *utf_ptr;   /* current position in utf-text */
1658         char *desc_end;  /* points behind utf string     */
1659         utf *desc = m->descriptor; /* method-descriptor  */
1660
1661         utf_ptr  = desc->text;
1662         desc_end = utf_end(desc);
1663
1664         /* ignore parametertypes */
1665         while ((utf_ptr<desc_end) && utf_nextu2(&utf_ptr)!=')')
1666                 /* skip */ ;
1667
1668         return get_type(&utf_ptr,desc_end, false);
1669 }
1670
1671
1672 /*****************************************************************************/
1673 /*****************************************************************************/
1674
1675
1676 /*--------------------------------------------------------*/
1677 void printNativeCall(nativeCall nc) {
1678   int i,j;
1679
1680   printf("\n%s's Native Methods call:\n",nc.classname); fflush(stdout);
1681   for (i=0; i<nc.methCnt; i++) {  
1682       printf("\tMethod=%s %s\n",nc.methods[i].methodname, nc.methods[i].descriptor);fflush(stdout);
1683
1684     for (j=0; j<nc.callCnt[i]; j++) {  
1685         printf("\t\t<%i,%i>aCalled = %s %s %s\n",i,j,
1686         nc.methods[i].methodCalls[j].classname, 
1687         nc.methods[i].methodCalls[j].methodname, 
1688         nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1689       }
1690     }
1691   printf("-+++++--------------------\n");fflush(stdout);
1692 }
1693
1694 /*--------------------------------------------------------*/
1695 void printCompNativeCall(nativeCompCall nc) {
1696   int i,j;
1697   printf("printCompNativeCall BEGIN\n");fflush(stdout); 
1698   printf("\n%s's Native Comp Methods call:\n",nc.classname->text);fflush(stdout);
1699   utf_display(nc.classname); fflush(stdout);
1700   
1701   for (i=0; i<nc.methCnt; i++) {  
1702     printf("\tMethod=%s %s\n",nc.methods[i].methodname->text,nc.methods[i].descriptor->text);fflush(stdout);
1703     utf_display(nc.methods[i].methodname); fflush(stdout);
1704     utf_display(nc.methods[i].descriptor);fflush(stdout);
1705     printf("\n");fflush(stdout);
1706
1707     for (j=0; j<nc.callCnt[i]; j++) {  
1708       printf("\t\t<%i,%i>bCalled = ",i,j);fflush(stdout);
1709         utf_display(nc.methods[i].methodCalls[j].classname);fflush(stdout);
1710         utf_display(nc.methods[i].methodCalls[j].methodname); fflush(stdout);
1711         utf_display(nc.methods[i].methodCalls[j].descriptor);fflush(stdout);
1712         printf("\n");fflush(stdout);
1713       }
1714     }
1715 printf("---------------------\n");fflush(stdout);
1716 }
1717
1718
1719 /*--------------------------------------------------------*/
1720 classMeth findNativeMethodCalls(utf *c, utf *m, utf *d ) 
1721 {
1722     int i = 0;
1723     int j = 0;
1724     int cnt = 0;
1725     classMeth mc;
1726     mc.i_class = i;
1727     mc.j_method = j;
1728     mc.methCnt = cnt;
1729
1730     return mc;
1731 }
1732
1733 /*--------------------------------------------------------*/
1734 nativeCall* findNativeClassCalls(char *aclassname ) {
1735 int i;
1736
1737 for (i=0;i<NATIVECALLSSIZE; i++) {
1738    /* convert table to utf later to speed up search */ 
1739    if (strcmp(nativeCalls[i].classname, aclassname) == 0) 
1740         return &nativeCalls[i];
1741    }
1742
1743 return NULL;
1744 }
1745 /*--------------------------------------------------------*/
1746 /*--------------------------------------------------------*/
1747 void utfNativeCall(nativeCall nc, nativeCompCall *ncc) {
1748   int i,j;
1749
1750
1751   ncc->classname = utf_new_char(nc.classname); 
1752   ncc->methCnt = nc.methCnt;
1753   
1754   for (i=0; i<nc.methCnt; i++) {  
1755     ncc->methods[i].methodname = utf_new_char(nc.methods[i].methodname);
1756     ncc->methods[i].descriptor = utf_new_char(nc.methods[i].descriptor);
1757     ncc->callCnt[i] = nc.callCnt[i];
1758
1759     for (j=0; j<nc.callCnt[i]; j++) {  
1760
1761         ncc->methods[i].methodCalls[j].classname  = utf_new_char(nc.methods[i].methodCalls[j].classname);
1762
1763         if (strcmp("", nc.methods[i].methodCalls[j].methodname) != 0) {
1764           ncc->methods[i].methodCalls[j].methodname = utf_new_char(nc.methods[i].methodCalls[j].methodname);
1765           ncc->methods[i].methodCalls[j].descriptor = utf_new_char(nc.methods[i].methodCalls[j].descriptor);
1766           }
1767         else {
1768           ncc->methods[i].methodCalls[j].methodname = NULL;
1769           ncc->methods[i].methodCalls[j].descriptor = NULL;
1770           }
1771       }
1772     }
1773 }
1774
1775
1776
1777 /*--------------------------------------------------------*/
1778
1779 bool natcall2utf(bool natcallcompdone) {
1780 int i;
1781
1782 if (natcallcompdone) 
1783         return true;
1784
1785 for (i=0;i<NATIVECALLSSIZE; i++) {
1786    utfNativeCall  (nativeCalls[i], &nativeCompCalls[i]);  
1787    }
1788
1789 return true;
1790 }
1791
1792 /*--------------------------------------------------------*/
1793
1794
1795 java_objectarray *builtin_asm_createclasscontextarray(classinfo **end,classinfo **start)
1796 {
1797 #if defined(__GNUC__)
1798 #warning platform dependend
1799 #endif
1800         java_objectarray *tmpArray;
1801         int i;
1802         classinfo **current;
1803         classinfo *c;
1804         size_t size=(((size_t)start)-((size_t)end)) / sizeof (classinfo*);
1805         /*printf("end %p, start %p, size %ld\n",end,start,size);*/
1806         if (!class_java_lang_Class)
1807                 class_java_lang_Class = class_new(utf_new_char ("java/lang/Class"));
1808         if (!class_java_lang_SecurityManager)
1809                 class_java_lang_SecurityManager = class_new(utf_new_char ("java/lang/SecurityManager"));
1810         if (size>0) {
1811                 if (start==class_java_lang_SecurityManager) {
1812                         size--;
1813                         start--;
1814                 }
1815         }
1816         tmpArray=builtin_newarray(size, class_array_of(class_java_lang_Class)->vftbl);
1817
1818         for(i=0,current=start;i<size;i++,current--) {
1819                 c=*current;
1820 /*              printf("%d\n",i);
1821                 utf_display(c->name);*/
1822                 use_class_as_object(c);
1823                 tmpArray->data[i]=c;
1824         }
1825         return tmpArray;
1826
1827 }
1828
1829 java_lang_ClassLoader *builtin_asm_getclassloader(classinfo **end,classinfo **start)
1830 {
1831 #if defined(__GNUC__)
1832 #warning platform dependend
1833 #endif
1834         int i;
1835         classinfo **current;
1836         classinfo *c;
1837         classinfo *privilegedAction;
1838         size_t size=(((size_t)start)-((size_t)end)) / sizeof (classinfo*);
1839 /*      log_text("builtin_asm_getclassloader");
1840         printf("end %p, start %p, size %ld\n",end,start,size);*/
1841
1842         if (!class_java_lang_SecurityManager)
1843                 class_java_lang_SecurityManager = class_new(utf_new_char ("java/lang/SecurityManager"));
1844         if (size>0) {
1845                 if (start==class_java_lang_SecurityManager) {
1846                         size--;
1847                         start--;
1848                 }
1849         }
1850
1851         privilegedAction=class_new(utf_new_char("java/security/PrivilegedAction"));
1852
1853         for(i=0,current=start;i<size;i++,current--) {
1854                 c=*current;
1855                 if (c==privilegedAction) return NULL;
1856                 if (c->classloader) return c->classloader;
1857         }
1858         return NULL;
1859
1860
1861
1862
1863 /*
1864         log_text("Java_java_lang_VMSecurityManager_currentClassLoader");
1865         init_systemclassloader();
1866
1867         return SystemClassLoader;*/
1868 }
1869
1870 /*
1871  * These are local overrides for various environment variables in Emacs.
1872  * Please do not remove this and leave it at the end of the file, where
1873  * Emacs will automagically detect them.
1874  * ---------------------------------------------------------------------
1875  * Local variables:
1876  * mode: c
1877  * indent-tabs-mode: t
1878  * c-basic-offset: 4
1879  * tab-width: 4
1880  * End:
1881  */