fixed builtin problem on alpha
[cacao.git] / headers.c
1 /* headers.c - main for header generation (cacaoh)
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
29    Changes: Mark Probst
30             Philipp Tomsich
31
32    $Id: headers.c 771 2003-12-13 23:11:08Z stefan $
33
34 */
35
36
37 #include <stdlib.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include "config.h"
41 #include "global.h"
42 #include "tables.h"
43 #include "loader.h"
44 #include "toolbox/chain.h"
45 #include "toolbox/memory.h"
46 #include "toolbox/loging.h"
47
48
49 /* replace command line options */
50
51 bool verbose =  false;
52 bool compileall = false;
53 bool runverbose = false;
54 bool collectverbose = false;
55
56 bool loadverbose = false;
57 bool linkverbose = false;
58 bool initverbose = false;
59
60 bool opt_rt = false;            /* true if RTA parse should be used     RT-CO */
61 bool opt_xta = false;           /* true if XTA parse should be used    XTA-CO */
62 bool opt_vta = false;           /* true if VTA parse should be used    VTA-CO */
63
64 bool showmethods = false;
65 bool showconstantpool = false;
66 bool showutf = false;
67
68 bool makeinitializations = true;
69
70 bool getloadingtime = false;
71 s8 loadingtime = 0;
72
73
74 /******* replace some external functions  *********/
75  
76 functionptr native_findfunction(utf *cname, utf *mname, utf *desc, bool isstatic)
77 { return NULL; }
78
79 java_objectheader *javastring_new(utf *text)
80 { return NULL; }
81
82
83 void throw_noclassdeffounderror_message(utf* classname)
84
85         printf("Class not found: ");
86         utf_display(classname);
87         printf("\n");
88         exit(1);
89 }
90
91
92 /* this is here to avoid link errors. 
93    We are not interested in linkagerrors in cacaoh right now
94 */
95 void throw_linkageerror_message(utf* classname) 
96
97 }
98
99
100 java_objectheader *literalstring_new(utf *u) { return NULL; }  
101
102
103 void literalstring_free(java_objectheader *o) { }
104 void stringtable_update() { }
105 void synchronize_caches() { }
106 void asm_call_jit_compiler() { }
107 void asm_calljavafunction() { }
108 void asm_dumpregistersandcall() { }
109 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
110
111 s4 asm_builtin_idiv(s4 a, s4 b) {return 0;}
112 s4 asm_builtin_irem(s4 a, s4 b) {return 0;}
113 s8 asm_builtin_ldiv(s8 a, s8 b) {return 0;}
114 s8 asm_builtin_lrem(s8 a, s8 b) {return 0;}
115
116 s4 asm_builtin_f2i(float a) { return 0; }
117 s8 asm_builtin_f2l(float a) { return 0; }
118 s4 asm_builtin_d2i(double a) { return 0; }
119 s8 asm_builtin_d2l(double a) { return 0; }
120
121 void asm_builtin_monitorenter(java_objectheader *o) {}
122 void asm_builtin_monitorexit(java_objectheader *o) {}
123
124 s4 asm_builtin_checkarraycast(java_objectheader *o,arraydescriptor *d) {return 0;}
125
126 #if defined(__I386__)
127 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) { return 0; }
128 void asm_builtin_newarray(s4 size, vftbl *arrayvftbl) {}
129 #endif
130
131 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
132
133 u1 *createcompilerstub(methodinfo *m) {return NULL;}
134 u1 *createnativestub(functionptr f, methodinfo *m) {return NULL;}
135 u1 *oldcreatenativestub(functionptr f, methodinfo *m) {return NULL;}
136
137 void removecompilerstub(u1 *stub) {}
138 void removenativestub(u1 *stub) {}
139
140 void asm_perform_threadswitch(u1 **from, u1 **to) {}
141 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
142 void asm_switchstackandcall() {}
143 void asm_handle_builtin_exception(classinfo *c) {}
144
145 java_objectheader *native_new_and_init(void *p) { return NULL; }
146
147
148 /************************ global variables **********************/
149
150 java_objectheader *exceptionptr;
151
152 static chain *nativemethod_chain;    /* chain with native methods     */
153 static chain *nativeclass_chain;                               /* chain with processed classes  */      
154 static chain *ident_chain;        /* chain with method and field names in current class */
155 static FILE *file = NULL;
156 static u4 outputsize;
157 static bool dopadding;
158
159
160 static void printIDpart(int c) 
161 {
162         if ((c >= 'a' && c <= 'z')
163                 || (c >= 'A' && c <= 'Z')
164                 || (c >= '0' && c <= '9')
165                 || (c == '_'))
166                 putc(c, file);
167         else
168                 putc('_', file);
169 }
170
171
172 static void printID(utf *u)
173 {
174         char *utf_ptr = u->text;
175         int i;
176
177         for (i = 0; i < utf_strlen(u); i++) 
178                 printIDpart(utf_nextu2(&utf_ptr));
179 }
180
181
182 static void addoutputsize (int len)
183 {
184         u4 newsize,i;
185         if (!dopadding) return;
186
187         newsize = ALIGN(outputsize, len);
188         
189         for (i = outputsize; i < newsize; i++) fprintf(file, "   u1 pad%d\n", (int) i);
190         outputsize = newsize;
191 }
192
193
194 static char *printtype(char *utf_ptr)
195 {
196         u2 c;
197
198         switch (utf_nextu2(&utf_ptr)) {
199         case 'V': fprintf (file, "void");
200                 break;
201         case 'I':
202         case 'S':
203         case 'B':
204         case 'C':
205         case 'Z': addoutputsize (4);
206                 fprintf (file, "s4");
207                 break;
208         case 'J': addoutputsize (8);
209                 fprintf (file, "s8");
210                 break;
211         case 'F': addoutputsize (4);
212                 fprintf (file, "float");
213                 break;
214         case 'D': addoutputsize (8);
215                 fprintf (file, "double");
216                 break;
217         case '[':
218                 addoutputsize ( sizeof(java_arrayheader*) ); 
219                 switch (utf_nextu2(&utf_ptr)) {
220                 case 'I':  fprintf (file, "java_intarray*"); break;
221                 case 'J':  fprintf (file, "java_longarray*"); break;
222                 case 'Z':  fprintf (file, "java_booleanarray*"); break;
223                 case 'B':  fprintf (file, "java_bytearray*"); break;
224                 case 'S':  fprintf (file, "java_shortarray*"); break;
225                 case 'C':  fprintf (file, "java_chararray*"); break;
226                 case 'F':  fprintf (file, "java_floatarray*"); break;
227                 case 'D':  fprintf (file, "java_doublearray*"); break;
228                                 
229                 case '[':  fprintf (file, "java_objectarray*");                                        
230                         while ((c = utf_nextu2(&utf_ptr)) == '[') ;
231                         if (c=='L') 
232                                 while (utf_nextu2(&utf_ptr) != ';');
233                         break;
234                            
235                 case 'L':  fprintf (file, "java_objectarray*");
236                         while ( utf_nextu2(&utf_ptr) != ';');
237                         break;
238                 default: panic ("invalid type descriptor");
239                 }
240                 break;
241                 
242         case 'L': 
243                 addoutputsize ( sizeof(java_objectheader*));
244                 fprintf (file, "struct ");
245                 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);     
246                 fprintf (file, "*");
247                 break;
248                                         
249         default:  panic ("Unknown type in field descriptor");
250         }
251         
252         return utf_ptr;
253 }
254
255
256 /******* determine the number of entries of a utf string in the ident chain *****/
257
258 static int searchidentchain_utf(utf *ident) 
259 {
260         utf *u = chain_first(ident_chain);     /* first element of list */
261         int count = 0;
262
263         while (u) {
264                 if (u==ident) count++;         /* string found */
265                 u = chain_next(ident_chain);   /* next element in list */ 
266         }
267
268         return count;
269 }
270
271
272 /**************** print structure for direct access to objects ******************/      
273
274 static void printfields(classinfo *c)
275 {
276         u4 i;
277         fieldinfo *f;
278         int ident_count;
279         
280         if (!c) {
281                 addoutputsize(sizeof(java_objectheader));
282                 fprintf(file, "   java_objectheader header;\n");
283                 return;
284         }
285                 
286         printfields(c->super);
287         
288         for (i = 0; i < c->fieldscount; i++) {
289                 f = &(c->fields[i]);
290                 
291                 if (!(f->flags & ACC_STATIC)) {
292                         fprintf(file, "   ");
293                         printtype(f->descriptor->text);
294                         fprintf(file, " ");
295                         utf_fprint(file, f->name);
296
297                         /* rename multiple fieldnames */
298                         if ((ident_count = searchidentchain_utf(f->name)))
299                                 fprintf(file, "%d", ident_count - 1);
300                         chain_addlast(ident_chain, f->name);    
301
302                         fprintf(file, ";\n");
303                 }
304         }
305 }
306
307
308 /***************** store prototype for native method in file ******************/
309
310 static void printmethod(methodinfo *m)
311 {
312         char *utf_ptr;
313         u2 paramnum = 1;
314         u2 ident_count;
315
316         /* search for return-type in descriptor */      
317         utf_ptr = m->descriptor->text;
318         while (utf_nextu2(&utf_ptr) != ')');
319
320         /* create remarks */
321         fprintf(file, "\n/*\n * Class:     ");
322         utf_fprint(file, m->class->name);
323         fprintf(file, "\n * Method:    ");
324         utf_fprint(file, m->name);
325         fprintf(file, "\n * Signature: ");
326         utf_fprint(file, m->descriptor);
327         fprintf(file, "\n */\n");       
328
329         /* create prototype */                  
330         fprintf(file, "JNIEXPORT ");                            
331         printtype(utf_ptr);
332         fprintf(file, " JNICALL Java_");
333         printID(m->class->name);           
334
335         /* rename overloaded method */
336         if ((ident_count = searchidentchain_utf(m->name)))
337                 fprintf(file, "%d", ident_count - 1);           
338         chain_addlast(ident_chain, m->name);    
339
340         fprintf(file, "_");
341         printID(m->name);
342         fprintf(file, "(JNIEnv *env");
343         
344         utf_ptr = m->descriptor->text + 1;
345                         
346         if (!(m->flags & ACC_STATIC)) {
347                 fprintf(file, ", struct ");
348                 printID(m->class->name);
349                 fprintf(file, "* this");
350
351         } else {
352                 fprintf(file, ", jclass clazz");
353         }
354
355         if ((*utf_ptr) != ')') fprintf(file, ", ");
356                         
357         while ((*utf_ptr) != ')') {
358                 utf_ptr = printtype(utf_ptr);
359                 fprintf(file, " par%d", paramnum++);
360                 if ((*utf_ptr)!=')') fprintf(file, ", ");
361         }
362                         
363         fprintf(file, ");\n\n");
364 }
365
366
367 /****************** remove package-name in fully-qualified classname *********************/
368
369 static void gen_header_filename(char *buffer, utf *u)
370 {
371         int i;
372   
373         for (i = 0;i < utf_strlen(u); i++) {
374                 if ((u->text[i] == '/') || (u->text[i] == '$')) buffer[i] = '_';  /* convert '$' and '/' to '_' */
375                 else buffer[i] = u->text[i];
376         }
377         buffer[utf_strlen(u)] = '\0';
378 }
379
380
381 /*********** create headerfile for classes and store native methods in chain ************/
382
383 static void headerfile_generate(classinfo *c)
384 {
385         char header_filename[1024] = "";
386         char classname[1024]; 
387         char uclassname[1024];
388         u2 i;
389         methodinfo *m;                  
390                       
391         /* store class in chain */                    
392         chain_addlast(nativeclass_chain, c);                                                            
393                                 
394         /* open headerfile for class */
395         gen_header_filename(classname, c->name);
396
397         /* create chain for renaming fields */
398         ident_chain = chain_new();
399         
400         sprintf(header_filename, "nat/%s.h", classname);
401         file = fopen(header_filename, "w");
402         if (!file) panic("Can not open file to store header information");
403
404         fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
405
406         /* convert to uppercase */
407         for (i = 0; classname[i]; i++) {
408                 uclassname[i] = toupper(classname[i]);
409         }
410         uclassname[i] = '\0';
411
412         fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
413
414         /* create structure for direct access to objects */     
415         fprintf(file, "/* Structure information for class: ");
416         utf_fprint(file, c->name);
417         fprintf(file, " */\n\n");
418         fprintf(file, "typedef struct ");
419         printID(c->name);                                                       
420         fprintf(file, " {\n");
421         outputsize = 0;
422         dopadding = true;
423         printfields(c);
424         fprintf(file, "} ");
425         printID(c->name);
426         fprintf(file, ";\n\n");
427
428         /* create chain for renaming overloaded methods */
429         chain_free(ident_chain);
430         ident_chain = chain_new();
431
432         /* create method-prototypes */
433                                 
434         for (i = 0; i < c->methodscount; i++) {
435
436                 m = &(c->methods[i]);
437
438                 if (m->flags & ACC_NATIVE) {
439                         chain_addlast(nativemethod_chain, m);
440                         printmethod(m);
441                 }
442         }
443
444         chain_free(ident_chain);
445
446         fprintf(file, "#endif\n\n");
447
448         fclose(file);
449 }
450
451
452 /******** print classname, '$' used to seperate inner-class name ***********/
453
454 void print_classname(classinfo *clazz)
455 {
456         utf *u = clazz->name;
457     char *endpos  = u->text + u->blength;
458     char *utf_ptr = u->text; 
459         u2 c;
460
461     while (utf_ptr < endpos) {
462                 if ((c = utf_nextu2(&utf_ptr)) == '_') {
463                         putc('$', file);
464
465                 } else {
466                         putc(c, file);
467                 }
468         }
469
470
471
472 /*************** create table for locating native functions ****************/
473
474 static void printnativetableentry(methodinfo *m)
475 {
476         fprintf(file, "   { \"");
477         print_classname(m->class);
478         fprintf(file, "\",\n     \"");
479         utf_fprint(file, m->name);
480         fprintf(file, "\",\n     \"");
481         utf_fprint(file, m->descriptor);
482         fprintf(file, "\",\n     ");
483
484         if ((m->flags & ACC_STATIC) != 0)
485                 fprintf(file, "true");
486         else
487                 fprintf(file, "false");
488
489         fprintf(file, ",\n     ");
490         fprintf(file, "(functionptr) Java_");
491         printID(m->class->name);
492         fprintf(file,"_");
493         printID(m->name);
494         fprintf(file,"\n   },\n");
495 }
496
497
498 /***************************************************************************
499
500         create the nativetypes-headerfile which includes 
501         the headerfiles of the classes stored in the classes-chain 
502
503 ****************************************************************************/
504
505 static void headers_finish()
506 {
507         methodinfo *m;
508         classinfo *c;
509         char classname[1024];
510         
511         file = fopen("nativetypes.hh", "w");
512
513         if (!file)
514                 panic("Can not open file 'native.h' to store header information");
515         
516         fprintf(file, "/* Headerfile for native methods: nativetypes.hh */\n");
517         fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");      
518         fprintf(file, "\n/* include native-Headerfiles */\n\n");
519                         
520         c = chain_first(nativeclass_chain);
521         while (c) {
522                 dopadding = false;
523                 gen_header_filename(classname, c->name);                                                                                                                                                                                        
524                 fprintf(file, "#include \"nat/%s.h\"\n", classname);            
525                 c = chain_next(nativeclass_chain);              
526         }
527
528     fclose(file);
529         chain_free(nativeclass_chain);
530         
531         /* create table of native-methods */
532
533         file = fopen("nativetable.hh", "w");
534
535         if (!file)
536                 panic("Can not open file 'nativetable' to store native-link-table");
537
538         fprintf(file, "/* Table of native methods: nativetables.hh */\n");
539         fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n"); 
540
541         while ((m = chain_first(nativemethod_chain)) != NULL) {
542                 chain_remove(nativemethod_chain);               
543                 printnativetableentry(m);
544         }
545                 
546         chain_free(nativemethod_chain);
547         fclose(file);
548 }
549
550
551 /******************** internal function: print_usage ************************
552
553 Prints usage information for the JAVA header generator to stdout.
554
555 ***************************************************************************/
556
557 static void print_usage()
558 {
559         printf("Usage: cacaoh class [class..]\n");
560 }   
561
562
563
564 /************************** Function: main *******************************
565
566    Main program.
567    
568 **************************************************************************/
569
570 int main(int argc, char **argv)
571 {
572         s4 i,a;
573         char *cp;
574         classinfo *topclass;
575         void *dummy;
576                 
577
578         /********** internal (only used by main) *****************************/
579    
580         char classpath[500] = "";
581         char offsets_filename[1024] = ""; /* phil */
582         u4 heapsize = 100000;
583
584         /*********** options so only headers are generated *******************/
585    
586         makeinitializations = false;
587    
588
589         /************ Collect some info from the environment *****************/
590
591         cp = getenv("CLASSPATH");
592         if (cp) {
593                 strcpy(classpath + strlen(classpath), ":");
594                 strcpy(classpath + strlen(classpath), cp);
595         }
596
597         if (argc < 2) {
598                 print_usage();
599                 exit(10);
600         }
601
602
603         /**************************** Program start **************************/
604
605         log_init(NULL);
606         log_text("Java - header-generator started"); 
607         
608         sprintf(offsets_filename, "jit/%s/offsets.h", ARCH_DIR);
609         file = fopen(offsets_filename, "w");
610         if (file == NULL) {
611                 fprintf(stderr, "Can not open file '%s' for write", offsets_filename);
612                 exit(-1);
613         }
614         
615         fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n"); 
616
617         fprintf(file, "#define offobjvftbl    %3d\n", (int) OFFSET(java_objectheader, vftbl));
618         fprintf(file, "#define offarraysize   %3d\n", (int) OFFSET(java_arrayheader, size));
619         fprintf(file, "#define offobjarrdata  %3d\n\n", (int) OFFSET(java_objectarray, data[0]));
620         fprintf(file, "#define offbaseval     %3d\n", (int) OFFSET(vftbl, baseval));
621         fprintf(file, "#define offdiffval     %3d\n\n", (int) OFFSET(vftbl, diffval));
622
623         fprintf(file, "#define offclassvftbl    %3d\n", (int) OFFSET(classinfo, vftbl));
624
625         fprintf(file, "#define offjniitemtype %3d\n", (int) OFFSET(jni_callblock, itemtype));
626         fprintf(file, "#define offjniitem     %3d\n", (int) OFFSET(jni_callblock, item));
627         fprintf(file, "#define sizejniblock   %3d\n\n", (int) sizeof(jni_callblock));
628
629
630         fclose(file);
631
632         suck_init(classpath);
633    
634         tables_init();
635         heap_init(heapsize, heapsize, &dummy);
636         loader_init();
637
638
639         /*********************** Load JAVA classes  **************************/
640         
641         nativemethod_chain = chain_new();
642         nativeclass_chain = chain_new();
643         
644         for (a = 1; a < argc; a++) {
645                 cp = argv[a];
646
647                 /* convert classname */
648                 for (i = strlen(cp) - 1; i >= 0; i--) {
649                         switch (cp[i]) {
650                         case '.': cp[i]='/';
651                                 break;
652                         case '_': cp[i]='$';    
653                         }
654                 }
655         
656                 topclass = loader_load(utf_new_char(cp));
657                 
658         headerfile_generate(topclass);
659         }
660
661         headers_finish();
662
663         /************************ Release all resources **********************/
664
665         loader_close();
666         heap_close();
667         tables_close(literalstring_free);
668         
669
670         /* Print "finished" message */
671
672         log_text("Java - header-generator stopped");
673         log_cputime();
674         mem_usagelog(1);
675         
676         return 0;
677 }
678
679
680 void setVMClassField(classinfo *c)
681 {
682 }
683
684
685 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this) {
686         return 0;
687 }
688
689
690 /*
691  * These are local overrides for various environment variables in Emacs.
692  * Please do not remove this and leave it at the end of the file, where
693  * Emacs will automagically detect them.
694  * ---------------------------------------------------------------------
695  * Local variables:
696  * mode: c
697  * indent-tabs-mode: t
698  * c-basic-offset: 4
699  * tab-width: 4
700  * End:
701  */