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