* src/vm/class.c: Java ME changes.
[cacao.git] / src / cacaoh / headers.c
1 /* src/cacaoh/headers.c - functions for header generation
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
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., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Reinhard Grafl
28             Mark Probst
29             Philipp Tomsich
30             Christian Thalinger
31             Edwin Steiner
32
33    $Id: headers.c 6244 2006-12-27 15:15:31Z twisti $
34
35 */
36
37
38 #include "config.h"
39
40 #include <assert.h>
41 #include <ctype.h>
42 #include <stdarg.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "vm/types.h"
47
48 #if defined(ENABLE_THREADS)
49 # if defined(__DARWIN__)
50 #  include <signal.h>
51 # endif
52 # include <ucontext.h>
53 #endif
54
55 #include "mm/gc-common.h"
56 #include "mm/memory.h"
57 #include "native/include/java_lang_String.h"
58 #include "native/include/java_lang_Throwable.h"
59 #include "toolbox/chain.h"
60 #include "toolbox/logging.h"
61 #include "vm/builtin.h"
62 #include "vm/class.h"
63 #include "vm/global.h"
64 #include "vm/method.h"
65 #include "vm/loader.h"
66 #include "vm/options.h"
67 #include "vm/stringlocal.h"
68 #include "vm/jit/asmpart.h"
69
70
71 /* Invocation API variables ***************************************************/
72
73 _Jv_JavaVM *_Jv_jvm;                    /* denotes a Java VM                  */
74 char       *_Jv_bootclasspath;
75
76 char       *cacao_prefix;
77 char       *classpath_libdir;
78
79 char       *_Jv_classpath;
80 char       *_Jv_java_library_path;
81
82 #if defined(ENABLE_INTRP)
83 /* dummy interpreter stack to keep the compiler happy */
84
85 u1 *intrp_main_stack;
86 #endif
87
88
89 #if !defined(NDEBUG)
90 /* dummy variable */
91 bool inline_debug_log = false;
92 #endif
93
94
95 /* for raising exceptions from native methods *********************************/
96
97 #if !defined(ENABLE_THREADS)
98 java_objectheader *_no_threads_exceptionptr = NULL;
99 #endif
100
101
102 /* replace some non-vmcore functions ******************************************/
103
104 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
105                                                                 bool isstatic)
106 {
107         /* return something different than NULL, otherwise we get an exception */
108
109         return (functionptr) 1;
110 }
111
112 java_objectheader *native_new_and_init(classinfo *c) { return NULL; }
113 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s) { return NULL; }
114 java_objectheader *native_new_and_init_int(classinfo *c, s4 i) { return NULL; }
115 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t) { return NULL; }
116
117
118 java_objectheader *vm_call_method(methodinfo *m, java_objectheader *o, ...)
119 { return NULL; }
120
121 void vm_abort(const char *text, ...)
122 {
123         va_list ap;
124
125         /* print the log message */
126
127         va_start(ap, text);
128         vfprintf(stderr, text, ap);
129         va_end(ap);
130
131         /* now abort the VM */
132
133         abort();
134 }
135
136 /* code patching functions */
137 void patcher_builtin_arraycheckcast(u1 *sp) {}
138
139 #if defined(__MIPS__)
140 long compare_and_swap(long *p, long oldval, long newval)
141 {
142         if (*p == oldval) {
143                 *p = newval;
144                 return oldval;
145         } else
146                 return *p;
147 }
148 #endif
149
150
151 u1 *createcompilerstub(methodinfo *m) { return NULL; }
152 #if defined(ENABLE_INTRP)
153 u1 *intrp_createcompilerstub(methodinfo *m) { return NULL; }
154 #endif
155
156 codeinfo *codegen_createnativestub(functionptr f, methodinfo *m) { return NULL; }
157
158 void removecompilerstub(u1 *stub) {}
159 void removenativestub(u1 *stub) {}
160
161 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
162 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
163
164 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
165
166 void asm_handle_builtin_exception(classinfo *c) {}
167
168 #if defined(ENABLE_JIT)
169 void asm_abstractmethoderror(void) {}
170 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
171 #endif
172
173 #if defined(ENABLE_INTRP)
174 void intrp_asm_abstractmethoderror(void) {}
175 void intrp_asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
176 #endif
177
178 u8 asm_get_cycle_count(void)
179 {
180         return 0;
181 }
182
183
184 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
185 {
186         return NULL;
187 }
188
189 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
190 {
191         return typecheck_TRUE;
192 }
193
194 void typeinfo_init_classinfo(typeinfo *info,classinfo *c)
195 {
196 }
197
198 bool typeinfo_init_class(typeinfo *info,classref_or_classinfo c)
199 {
200         return true;
201 }
202
203 void typeinfo_print(FILE *file,typeinfo *info,int indent) {}
204 void typeinfo_print_short(FILE *file,typeinfo *info) {}
205
206 void exceptions_print_exception(java_objectheader *xptr) {}
207 void stacktrace_dump_trace(threadobject *thread) {}
208 void stacktrace_print_trace(java_objectheader *xptr) {}
209 java_objectarray *stacktrace_getClassContext() { return NULL; }
210 void code_free_code_of_method(methodinfo *m) {}
211
212 void jit_invalidate_code(methodinfo *m) {}
213
214
215 /* exception functions ********************************************************/
216
217 /* these should not be called */
218
219 void throw_main_exception_exit(void) { assert(0); }
220 void throw_exception(void) { assert(0); }
221 void throw_exception_exit(void) { assert(0); }
222
223 void exceptions_throw_verifyerror(methodinfo *m, const char *message)
224 {
225         assert(0);
226 }
227
228 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
229 {
230         assert(0);
231
232         /* keep compiler happy */
233
234         return NULL;
235 }
236
237
238 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
239 {
240         va_list ap;
241
242         fprintf(stderr, "%s: ", exception);
243
244         va_start(ap, message);
245         vfprintf(stderr, message, ap);
246         va_end(ap);
247
248         fputc('\n', stderr);
249
250         exit(1);
251 }
252
253
254 void exceptions_throw_outofmemory_exit(void)
255 {
256         fprintf(stderr, "java.lang.InternalError: Out of memory\n");
257         exit(1);
258 }
259
260
261 java_objectheader *new_exception(const char *classname)
262 {
263         fprintf(stderr, "%s\n", classname);
264         exit(1);
265
266         /* keep compiler happy */
267
268         return NULL;
269 }
270
271
272 java_objectheader *new_exception_message(const char *classname, const char *message)
273 {
274         fprintf(stderr, "%s: %s\n", classname, message);
275         exit(1);
276
277         /* keep compiler happy */
278
279         return NULL;
280 }
281
282
283 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
284 {
285         fprintf(stderr, "%s: ", classname);
286         utf_display_printable_ascii(message);
287         fputc('\n', stderr);
288
289         exit(1);
290
291         /* keep compiler happy */
292
293         return NULL;
294 }
295
296
297 java_objectheader *new_exception_javastring(const char *classname,
298                                                                                         java_lang_String *message)
299 {
300         fprintf(stderr, "%s: ", classname);
301         /* TODO print message */
302         fputc('\n', stderr);
303
304         exit(1);
305
306         /* keep compiler happy */
307
308         return NULL;
309 }
310
311
312 void exceptions_throw_abstractmethoderror(void)
313 {
314         fprintf(stderr, "java.lang.AbstractMethodError\n");
315
316         exit(1);
317 }
318
319
320 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
321 {
322         va_list ap;
323
324         utf_display_printable_ascii(c->name);
325         fprintf(stderr, ": ");
326
327         va_start(ap, message);
328         vfprintf(stderr, message, ap);
329         va_end(ap);
330
331         fputc('\n', stderr);
332
333         exit(1);
334
335         /* keep compiler happy */
336
337         return NULL;
338 }
339
340
341 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
342 {
343         va_list ap;
344
345         va_start(ap, message);
346         (void) new_classformaterror(c, message, ap);
347         va_end(ap);
348 }
349
350
351 java_objectheader *new_classnotfoundexception(utf *name)
352 {
353         fprintf(stderr, "java.lang.ClassNotFoundException: ");
354         utf_fprint_printable_ascii(stderr, name);
355         fputc('\n', stderr);
356
357         exit(1);
358
359         /* keep compiler happy */
360
361         return NULL;
362 }
363
364
365 java_objectheader *new_noclassdeffounderror(utf *name)
366 {
367         fprintf(stderr, "java.lang.NoClassDefFoundError: ");
368         utf_fprint_printable_ascii(stderr, name);
369         fputc('\n', stderr);
370
371         exit(1);
372
373         /* keep compiler happy */
374
375         return NULL;
376 }
377
378
379 java_objectheader *exceptions_new_linkageerror(const char *message,
380                                                                                            classinfo *c)
381 {
382         fprintf(stderr, "java.lang.LinkageError: %s",message);
383         if (c) {
384                 utf_fprint_printable_ascii_classname(stderr, c->name);
385         }
386         fputc('\n', stderr);
387
388         exit(1);
389
390         /* keep compiler happy */
391
392         return NULL;
393 }
394
395 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
396                                                                                                         utf *name, utf *desc)
397 {
398         fprintf(stderr, "java.lang.NoSuchMethodError: ");
399         utf_fprint_printable_ascii(stderr, c->name);
400         fprintf(stderr, ".");
401         utf_fprint_printable_ascii(stderr, name);
402         utf_fprint_printable_ascii(stderr, desc);
403         fputc('\n', stderr);
404
405         exit(1);
406
407         /* keep compiler happy */
408
409         return NULL;
410 }
411
412
413 void exceptions_throw_nosuchmethoderror(classinfo *c, utf *name, utf *desc)
414 {
415         (void) exceptions_new_nosuchmethoderror(c, name, desc);
416 }
417
418
419 java_objectheader *new_internalerror(const char *message, ...)
420 {
421         va_list ap;
422
423         fprintf(stderr, "%s: ", string_java_lang_InternalError);
424
425         va_start(ap, message);
426         vfprintf(stderr, message, ap);
427         va_end(ap);
428
429         exit(1);
430
431         /* keep compiler happy */
432
433         return NULL;
434 }
435
436
437 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
438 {
439         va_list ap;
440
441         fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
442
443         utf_display_printable_ascii(c->name);
444         fprintf(stderr, ": ");
445
446         va_start(ap, message);
447         vfprintf(stderr, message, ap);
448         va_end(ap);
449
450         exit(1);
451
452         /* keep compiler happy */
453
454         return NULL;
455 }
456
457
458 void exceptions_throw_virtualmachineerror(void)
459 {
460         fprintf(stderr, "%s", string_java_lang_VirtualMachineError);
461         exit(1);
462 }
463
464
465 void exceptions_throw_arrayindexoutofboundsexception(void)
466 {
467         fprintf(stderr, "%s", string_java_lang_ArrayIndexOutOfBoundsException);
468         exit(1);
469 }
470
471
472 java_objectheader *exceptions_new_arraystoreexception(void)
473 {
474         fprintf(stderr, "%s", string_java_lang_ArrayStoreException);
475         exit(1);
476
477         /* keep compiler happy */
478
479         return NULL;
480 }
481
482
483 void exceptions_throw_arraystoreexception(void)
484 {
485         (void) exceptions_new_arraystoreexception();
486 }
487
488
489 java_objectheader *exceptions_throw_illegalmonitorstateexception(void)
490 {
491         fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
492         exit(1);
493
494         /* keep compiler happy */
495
496         return NULL;
497 }
498
499
500 java_objectheader *new_negativearraysizeexception(void)
501 {
502         fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
503         exit(1);
504
505         /* keep compiler happy */
506
507         return NULL;
508 }
509
510
511 void exceptions_throw_negativearraysizeexception(void)
512 {
513         (void) new_negativearraysizeexception();
514 }
515
516
517 java_objectheader *exceptions_new_nullpointerexception(void)
518 {
519         fprintf(stderr, "%s", string_java_lang_NullPointerException);
520         exit(1);
521
522         /* keep compiler happy */
523
524         return NULL;
525 }
526
527
528 void exceptions_throw_nullpointerexception(void)
529 {
530         (void) exceptions_new_nullpointerexception();
531 }
532
533
534 void classnotfoundexception_to_noclassdeffounderror(void)
535 {
536 }
537
538 /* machine dependent stuff ****************************************************/
539
540 #if defined(ENABLE_THREADS)
541 critical_section_node_t asm_criticalsections;
542 void thread_restartcriticalsection(ucontext_t *uc) {}
543 #endif
544
545 void md_param_alloc(methoddesc *md) {}
546
547
548 #if defined(ENABLE_INTRP)
549 void print_dynamic_super_statistics(void) {}
550 #endif
551
552
553 /************************ global variables **********************/
554
555 #define ACC_NATIVELY_OVERLOADED    0x10000000
556
557 chain *ident_chain;     /* chain with method and field names in current class */
558 FILE *file = NULL;
559 static u4 outputsize;
560 static bool dopadding;
561
562
563 static void printIDpart(int c)
564 {
565         if ((c >= 'a' && c <= 'z') ||
566                 (c >= 'A' && c <= 'Z') ||
567                 (c >= '0' && c <= '9') ||
568                 (c == '_'))
569                 putc(c, file);
570         else
571                 putc('_', file);
572 }
573
574
575 void printID(utf *u)
576 {
577         char *utf_ptr = u->text;
578         int i;
579
580         for (i = 0; i < utf_get_number_of_u2s(u); i++) 
581                 printIDpart(utf_nextu2(&utf_ptr));
582 }
583
584
585 static void addoutputsize (int len)
586 {
587         u4 newsize,i;
588         if (!dopadding) return;
589
590         newsize = MEMORY_ALIGN(outputsize, len);
591         
592         for (i = outputsize; i < newsize; i++) fprintf(file, "   u1 pad%d\n", (int) i);
593         outputsize = newsize;
594 }
595
596
597 void printOverloadPart(utf *desc)
598 {
599         char *utf_ptr=desc->text;
600         u2 c;
601
602         fprintf(file, "__");
603
604         while ((c = utf_nextu2(&utf_ptr)) != ')') {
605                 switch (c) {
606                 case 'I':
607                 case 'S':
608                 case 'B':
609                 case 'C':
610                 case 'Z':
611                 case 'J':
612                 case 'F':
613                 case 'D': 
614                         fprintf(file, "%c", (char) c);
615                         break;
616                 case '[':
617                         fprintf(file, "_3");
618                         break;
619                 case 'L':
620                         putc('L', file);
621                         while ((c = utf_nextu2(&utf_ptr)) != ';')
622                                 printIDpart(c);
623                         fprintf(file, "_2");
624                         break;
625                 case '(':
626                         break;
627                 default: 
628                         log_text("invalid method descriptor");
629                         assert(0);
630                 }
631         }
632 }
633
634 static char *printtype(char *utf_ptr)
635 {
636         u2 c;
637
638         switch (utf_nextu2(&utf_ptr)) {
639         case 'V': fprintf (file, "void");
640                 break;
641         case 'I':
642         case 'S':
643         case 'B':
644         case 'C':
645         case 'Z': addoutputsize (4);
646                 fprintf (file, "s4");
647                 break;
648         case 'J': addoutputsize (8);
649                 fprintf (file, "s8");
650                 break;
651         case 'F': addoutputsize (4);
652                 fprintf (file, "float");
653                 break;
654         case 'D': addoutputsize (8);
655                 fprintf (file, "double");
656                 break;
657         case '[':
658                 addoutputsize ( sizeof(java_arrayheader*) ); 
659                 switch (utf_nextu2(&utf_ptr)) {
660                 case 'I':  fprintf (file, "java_intarray*"); break;
661                 case 'J':  fprintf (file, "java_longarray*"); break;
662                 case 'Z':  fprintf (file, "java_booleanarray*"); break;
663                 case 'B':  fprintf (file, "java_bytearray*"); break;
664                 case 'S':  fprintf (file, "java_shortarray*"); break;
665                 case 'C':  fprintf (file, "java_chararray*"); break;
666                 case 'F':  fprintf (file, "java_floatarray*"); break;
667                 case 'D':  fprintf (file, "java_doublearray*"); break;
668                                 
669                 case '[': fprintf(file, "java_objectarray*");
670                         while ((c = utf_nextu2(&utf_ptr)) == '[');
671                         if (c == 'L')
672                                 while (utf_nextu2(&utf_ptr) != ';');
673                         break;
674                            
675                 case 'L':  fprintf(file, "java_objectarray*");
676                         while (utf_nextu2(&utf_ptr) != ';');
677                         break;
678                 default:
679                         log_text("invalid type descriptor");
680                         assert(0);
681                 }
682                 break;
683                 
684         case 'L': 
685                 addoutputsize ( sizeof(java_objectheader*));
686                 fprintf (file, "struct ");
687                 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);     
688                 fprintf (file, "*");
689                 break;
690                                         
691         default:
692                 log_text("Unknown type in field descriptor");
693                 assert(0);
694         }
695         
696         return utf_ptr;
697 }
698
699
700 /***** determine the number of entries of a utf string in the ident chain *****/
701
702 static int searchidentchain_utf(utf *ident) 
703 {
704         utf *u = chain_first(ident_chain);     /* first element of list */
705         int count = 0;
706
707         while (u) {
708                 if (u==ident) count++;         /* string found */
709                 u = chain_next(ident_chain);   /* next element in list */ 
710         }
711
712         return count;
713 }
714
715
716 /************** print structure for direct access to objects ******************/
717
718 static void printfields(classinfo *c)
719 {
720         u4 i;
721         fieldinfo *f;
722         int ident_count;
723         
724         if (!c) {
725                 addoutputsize(sizeof(java_objectheader));
726                 fprintf(file, "   java_objectheader header;\n");
727                 return;
728         }
729                 
730         printfields(c->super.cls);
731         
732         for (i = 0; i < c->fieldscount; i++) {
733                 f = &(c->fields[i]);
734                 
735                 if (!(f->flags & ACC_STATIC)) {
736                         fprintf(file, "   ");
737                         printtype(f->descriptor->text);
738                         fprintf(file, " ");
739                         utf_fprint_printable_ascii(file, f->name);
740
741                         /* rename multiple fieldnames */
742                         if ((ident_count = searchidentchain_utf(f->name)))
743                                 fprintf(file, "%d", ident_count - 1);
744                         chain_addlast(ident_chain, f->name);    
745
746                         fprintf(file, ";\n");
747                 }
748         }
749 }
750
751
752 /***************** store prototype for native method in file ******************/
753
754 void printmethod(methodinfo *m)
755 {
756         char *utf_ptr;
757         u2 paramnum = 1;
758
759         /* search for return-type in descriptor */      
760         utf_ptr = m->descriptor->text;
761         while (utf_nextu2(&utf_ptr) != ')');
762
763         /* create remarks */
764         fprintf(file, "\n/*\n * Class:     ");
765         utf_fprint_printable_ascii(file, m->class->name);
766         fprintf(file, "\n * Method:    ");
767         utf_fprint_printable_ascii(file, m->name);
768         fprintf(file, "\n * Signature: ");
769         utf_fprint_printable_ascii(file, m->descriptor);
770         fprintf(file, "\n */\n");
771
772         /* create prototype */                  
773         fprintf(file, "JNIEXPORT ");
774         printtype(utf_ptr);
775         fprintf(file, " JNICALL Java_");
776         printID(m->class->name);
777
778         chain_addlast(ident_chain, m->name);
779
780         fprintf(file, "_");
781         printID(m->name);
782
783         /* ATTENTION: We use a dummy flag here. */
784
785         if (m->flags & ACC_NATIVELY_OVERLOADED)
786                 printOverloadPart(m->descriptor);
787
788         fprintf(file, "(JNIEnv *env");
789         
790         utf_ptr = m->descriptor->text + 1;
791                         
792         if (!(m->flags & ACC_STATIC)) {
793                 fprintf(file, ", struct ");
794                 printID(m->class->name);
795                 fprintf(file, "* this");
796
797         } else {
798                 fprintf(file, ", jclass clazz");
799         }
800
801         if ((*utf_ptr) != ')') fprintf(file, ", ");
802                         
803         while ((*utf_ptr) != ')') {
804                 utf_ptr = printtype(utf_ptr);
805                 fprintf(file, " par%d", paramnum++);
806                 if ((*utf_ptr)!=')') fprintf(file, ", ");
807         }
808                         
809         fprintf(file, ");\n\n");
810 }
811
812
813 /******* remove package-name in fully-qualified classname *********************/
814
815 void gen_header_filename(char *buffer, utf *u)
816 {
817         s4 i;
818   
819         for (i = 0; i < utf_get_number_of_u2s(u); i++) {
820                 if ((u->text[i] == '/') || (u->text[i] == '$')) {
821                         buffer[i] = '_';  /* convert '$' and '/' to '_' */
822
823                 } else {
824                         buffer[i] = u->text[i];
825                 }
826         }
827         buffer[utf_get_number_of_u2s(u)] = '\0';
828 }
829
830
831 /* create headerfile for classes and store native methods in chain ************/
832
833 void headerfile_generate(classinfo *c, char *opt_directory)
834 {
835         char header_filename[1024] = "";
836         char classname[1024]; 
837         char uclassname[1024];
838         u2 i;
839         methodinfo *m;                  
840         u2 j;
841         methodinfo *m2;
842         bool nativelyoverloaded;
843
844         /* prevent compiler warnings */
845
846         nativelyoverloaded = false;
847
848         /* open headerfile for class */
849         gen_header_filename(classname, c->name);
850
851         /* create chain for renaming fields */
852         ident_chain = chain_new();
853         
854         if (opt_directory) {
855                 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
856
857         } else {
858                 sprintf(header_filename, "%s.h", classname);
859         }
860
861         file = fopen(header_filename, "w");
862         if (!file) {
863                 log_text("Can not open file to store header information");
864                 assert(0);
865         }
866
867         fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
868
869         /* convert to uppercase */
870         for (i = 0; classname[i]; i++) {
871                 uclassname[i] = toupper(classname[i]);
872         }
873         uclassname[i] = '\0';
874
875         fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
876
877         /* create structure for direct access to objects */     
878         fprintf(file, "/* Structure information for class: ");
879         utf_fprint_printable_ascii(file, c->name);
880         fprintf(file, " */\n\n");
881         fprintf(file, "typedef struct ");
882         printID(c->name);                                                       
883         fprintf(file, " {\n");
884         outputsize = 0;
885         dopadding = true;
886
887         printfields(c);
888
889         fprintf(file, "} ");
890         printID(c->name);
891         fprintf(file, ";\n\n");
892
893         /* create chain for renaming overloaded methods */
894         chain_free(ident_chain);
895         ident_chain = chain_new();
896
897         /* create method-prototypes */
898                                 
899         /* find overloaded methods */
900
901         for (i = 0; i < c->methodscount; i++) {
902                 m = &(c->methods[i]);
903
904                 if (!(m->flags & ACC_NATIVE))
905                         continue;
906
907                 /* We use a dummy flag here. */
908
909                 if (!(m->flags & ACC_NATIVELY_OVERLOADED)) {
910                         nativelyoverloaded = false;
911
912                         for (j = i + 1; j < c->methodscount; j++) {
913                                 m2 = &(c->methods[j]);
914
915                                 if (!(m2->flags & ACC_NATIVE))
916                                         continue;
917
918                                 if (m->name == m2->name) {
919                                         m2->flags          |= ACC_NATIVELY_OVERLOADED;
920                                         nativelyoverloaded  = true;
921                                 }
922                         }
923                 }
924
925                 if (nativelyoverloaded == true)
926                         m->flags |= ACC_NATIVELY_OVERLOADED;
927         }
928
929         for (i = 0; i < c->methodscount; i++) {
930                 m = &(c->methods[i]);
931
932                 if (m->flags & ACC_NATIVE)
933                         printmethod(m);
934         }
935
936         chain_free(ident_chain);
937
938         fprintf(file, "#endif\n\n");
939
940         fclose(file);
941 }
942
943
944 /******** print classname, '$' used to seperate inner-class name ***********/
945
946 void print_classname(classinfo *clazz)
947 {
948         utf *u = clazz->name;
949     char *endpos  = u->text + u->blength;
950     char *utf_ptr = u->text; 
951         u2 c;
952
953     while (utf_ptr < endpos) {
954                 if ((c = utf_nextu2(&utf_ptr)) == '_')
955                         putc('$', file);
956                 else
957                         putc(c, file);
958         }
959
960
961 /* jvmti releated functions ************************************************/
962
963 #if defined(ENABLE_JVMTI)
964 void jvmti_ThreadStartEnd(int ev) {;}
965 void jvmti_ClassLoadPrepare(bool prepared, classinfo *c) {;}
966 void jvmti_MonitorContendedEntering(bool entered, jobject obj) {;}
967 #endif
968
969
970
971 /*
972  * These are local overrides for various environment variables in Emacs.
973  * Please do not remove this and leave it at the end of the file, where
974  * Emacs will automagically detect them.
975  * ---------------------------------------------------------------------
976  * Local variables:
977  * mode: c
978  * indent-tabs-mode: t
979  * c-basic-offset: 4
980  * tab-width: 4
981  * End:
982  * vim:noexpandtab:sw=4:ts=4:
983  */