1 /* headers.c - main for header generation (cacaoh)
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5 M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6 P. Tomsich, J. Wenninger
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Reinhard Grafl
32 $Id: headers.c 1029 2004-04-26 15:56:34Z twisti $
46 #include "toolbox/chain.h"
47 #include "toolbox/memory.h"
48 #include "toolbox/loging.h"
49 #include "nat/java_lang_String.h"
52 /* replace command line options */
55 bool verboseexception = false;
56 bool compileall = false;
57 bool runverbose = false;
58 bool collectverbose = false;
60 bool loadverbose = false;
61 bool linkverbose = false;
62 bool initverbose = false;
64 bool opt_rt = false; /* true if RTA parse should be used RT-CO */
65 bool opt_xta = false; /* true if XTA parse should be used XTA-CO */
66 bool opt_vta = false; /* true if VTA parse should be used VTA-CO */
67 bool opt_verify = true; /* true if classfiles should be verified */
68 bool opt_liberalutf = false; /* Don't check overlong UTF-8 sequences */
70 bool showmethods = false;
71 bool showconstantpool = false;
74 bool makeinitializations = true;
76 bool getloadingtime = false;
80 /******* replace some external functions *********/
82 functionptr native_findfunction(utf *cname, utf *mname, utf *desc, bool isstatic)
85 java_objectheader *javastring_new(utf *text)
88 utf *javastring_toutf(java_lang_String *string, bool isclassname)
92 /* some exception stuff */
94 char *string_java_lang_ClassCircularityError =
95 "java/lang/ClassCircularityError";
97 char *string_java_lang_ClassFormatError =
98 "java/lang/ClassFormatError";
100 char *string_java_lang_IllegalMonitorStateException =
101 "java/lang/IllegalMonitorStateException";
103 char *string_java_lang_LinkageError =
104 "java/lang/LinkageError";
106 char *string_java_lang_NegativeArraySizeException =
107 "java/lang/NegativeArraySizeException";
109 char *string_java_lang_NoClassDefFoundError =
110 "java/lang/NoClassDefFoundError";
112 char *string_java_lang_OutOfMemoryError =
113 "java/lang/OutOfMemoryError";
116 void throw_exception_exit() {}
117 void new_exception(char *classname) {}
118 void new_exception_message(char *classname, char *message) {}
119 void new_exception_throwable(char *classname, java_objectheader *t) {}
120 void new_exception_utfmessage(char *classname, utf *message)
122 printf("%s: ", classname);
123 utf_display(message);
128 java_objectheader *literalstring_new(utf *u) { return NULL; }
131 void literalstring_free(java_objectheader *o) { }
132 void stringtable_update() { }
133 void synchronize_caches() { }
134 void asm_call_jit_compiler() { }
135 void asm_calljavafunction() { }
136 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
138 s4 asm_builtin_idiv(s4 a, s4 b) {return 0;}
139 s4 asm_builtin_irem(s4 a, s4 b) {return 0;}
140 s8 asm_builtin_ldiv(s8 a, s8 b) {return 0;}
141 s8 asm_builtin_lrem(s8 a, s8 b) {return 0;}
143 s4 asm_builtin_f2i(float a) { return 0; }
144 s8 asm_builtin_f2l(float a) { return 0; }
145 s4 asm_builtin_d2i(double a) { return 0; }
146 s8 asm_builtin_d2l(double a) { return 0; }
148 void asm_builtin_monitorenter(java_objectheader *o) {}
149 void asm_builtin_monitorexit(java_objectheader *o) {}
151 s4 asm_builtin_checkarraycast(java_objectheader *obj, vftbl *target) {return 0;}
154 #if defined(__I386__)
155 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) { return 0; }
156 void asm_builtin_newarray(s4 size, vftbl *arrayvftbl) {}
159 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
161 u1 *createcompilerstub(methodinfo *m) {return NULL;}
162 u1 *createnativestub(functionptr f, methodinfo *m) {return NULL;}
163 u1 *oldcreatenativestub(functionptr f, methodinfo *m) {return NULL;}
165 void removecompilerstub(u1 *stub) {}
166 void removenativestub(u1 *stub) {}
168 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
169 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
170 void asm_switchstackandcall() {}
171 void asm_handle_builtin_exception(classinfo *c) {}
173 java_objectheader *native_new_and_init(void *p) { return NULL; }
176 /************************ global variables **********************/
178 THREADSPECIFIC java_objectheader *_exceptionptr;
180 static chain *nativemethod_chain; /* chain with native methods */
181 static chain *nativeclass_chain; /* chain with processed classes */
182 static chain *ident_chain; /* chain with method and field names in current class */
183 static FILE *file = NULL;
184 static u4 outputsize;
185 static bool dopadding;
188 static void printIDpart(int c)
190 if ((c >= 'a' && c <= 'z')
191 || (c >= 'A' && c <= 'Z')
192 || (c >= '0' && c <= '9')
200 static void printID(utf *u)
202 char *utf_ptr = u->text;
205 for (i = 0; i < utf_strlen(u); i++)
206 printIDpart(utf_nextu2(&utf_ptr));
210 static void addoutputsize (int len)
213 if (!dopadding) return;
215 newsize = ALIGN(outputsize, len);
217 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
218 outputsize = newsize;
222 static char *printtype(char *utf_ptr)
226 switch (utf_nextu2(&utf_ptr)) {
227 case 'V': fprintf (file, "void");
233 case 'Z': addoutputsize (4);
234 fprintf (file, "s4");
236 case 'J': addoutputsize (8);
237 fprintf (file, "s8");
239 case 'F': addoutputsize (4);
240 fprintf (file, "float");
242 case 'D': addoutputsize (8);
243 fprintf (file, "double");
246 addoutputsize ( sizeof(java_arrayheader*) );
247 switch (utf_nextu2(&utf_ptr)) {
248 case 'I': fprintf (file, "java_intarray*"); break;
249 case 'J': fprintf (file, "java_longarray*"); break;
250 case 'Z': fprintf (file, "java_booleanarray*"); break;
251 case 'B': fprintf (file, "java_bytearray*"); break;
252 case 'S': fprintf (file, "java_shortarray*"); break;
253 case 'C': fprintf (file, "java_chararray*"); break;
254 case 'F': fprintf (file, "java_floatarray*"); break;
255 case 'D': fprintf (file, "java_doublearray*"); break;
257 case '[': fprintf (file, "java_objectarray*");
258 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
260 while (utf_nextu2(&utf_ptr) != ';');
263 case 'L': fprintf (file, "java_objectarray*");
264 while ( utf_nextu2(&utf_ptr) != ';');
266 default: panic ("invalid type descriptor");
271 addoutputsize ( sizeof(java_objectheader*));
272 fprintf (file, "struct ");
273 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
277 default: panic ("Unknown type in field descriptor");
284 /***** determine the number of entries of a utf string in the ident chain *****/
286 static int searchidentchain_utf(utf *ident)
288 utf *u = chain_first(ident_chain); /* first element of list */
292 if (u==ident) count++; /* string found */
293 u = chain_next(ident_chain); /* next element in list */
300 /************** print structure for direct access to objects ******************/
302 static void printfields(classinfo *c)
309 addoutputsize(sizeof(java_objectheader));
310 fprintf(file, " java_objectheader header;\n");
314 printfields(c->super);
316 for (i = 0; i < c->fieldscount; i++) {
319 if (!(f->flags & ACC_STATIC)) {
321 printtype(f->descriptor->text);
323 utf_fprint(file, f->name);
325 /* rename multiple fieldnames */
326 if ((ident_count = searchidentchain_utf(f->name)))
327 fprintf(file, "%d", ident_count - 1);
328 chain_addlast(ident_chain, f->name);
330 fprintf(file, ";\n");
336 /***************** store prototype for native method in file ******************/
338 static void printmethod(methodinfo *m)
344 /* search for return-type in descriptor */
345 utf_ptr = m->descriptor->text;
346 while (utf_nextu2(&utf_ptr) != ')');
349 fprintf(file, "\n/*\n * Class: ");
350 utf_fprint(file, m->class->name);
351 fprintf(file, "\n * Method: ");
352 utf_fprint(file, m->name);
353 fprintf(file, "\n * Signature: ");
354 utf_fprint(file, m->descriptor);
355 fprintf(file, "\n */\n");
357 /* create prototype */
358 fprintf(file, "JNIEXPORT ");
360 fprintf(file, " JNICALL Java_");
361 printID(m->class->name);
363 /* rename overloaded method */
364 if ((ident_count = searchidentchain_utf(m->name)))
365 fprintf(file, "%d", ident_count - 1);
366 chain_addlast(ident_chain, m->name);
370 fprintf(file, "(JNIEnv *env");
372 utf_ptr = m->descriptor->text + 1;
374 if (!(m->flags & ACC_STATIC)) {
375 fprintf(file, ", struct ");
376 printID(m->class->name);
377 fprintf(file, "* this");
380 fprintf(file, ", jclass clazz");
383 if ((*utf_ptr) != ')') fprintf(file, ", ");
385 while ((*utf_ptr) != ')') {
386 utf_ptr = printtype(utf_ptr);
387 fprintf(file, " par%d", paramnum++);
388 if ((*utf_ptr)!=')') fprintf(file, ", ");
391 fprintf(file, ");\n\n");
395 /******* remove package-name in fully-qualified classname *********************/
397 static void gen_header_filename(char *buffer, utf *u)
401 for (i = 0;i < utf_strlen(u); i++) {
402 if ((u->text[i] == '/') || (u->text[i] == '$')) buffer[i] = '_'; /* convert '$' and '/' to '_' */
403 else buffer[i] = u->text[i];
405 buffer[utf_strlen(u)] = '\0';
409 /* create headerfile for classes and store native methods in chain ************/
411 static void headerfile_generate(classinfo *c)
413 char header_filename[1024] = "";
414 char classname[1024];
415 char uclassname[1024];
419 /* store class in chain */
420 chain_addlast(nativeclass_chain, c);
422 /* open headerfile for class */
423 gen_header_filename(classname, c->name);
425 /* create chain for renaming fields */
426 ident_chain = chain_new();
428 sprintf(header_filename, "nat/%s.h", classname);
429 file = fopen(header_filename, "w");
430 if (!file) panic("Can not open file to store header information");
432 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
434 /* convert to uppercase */
435 for (i = 0; classname[i]; i++) {
436 uclassname[i] = toupper(classname[i]);
438 uclassname[i] = '\0';
440 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
442 /* create structure for direct access to objects */
443 fprintf(file, "/* Structure information for class: ");
444 utf_fprint(file, c->name);
445 fprintf(file, " */\n\n");
446 fprintf(file, "typedef struct ");
448 fprintf(file, " {\n");
456 fprintf(file, ";\n\n");
458 /* create chain for renaming overloaded methods */
459 chain_free(ident_chain);
460 ident_chain = chain_new();
462 /* create method-prototypes */
464 for (i = 0; i < c->methodscount; i++) {
466 m = &(c->methods[i]);
468 if (m->flags & ACC_NATIVE) {
469 chain_addlast(nativemethod_chain, m);
474 chain_free(ident_chain);
476 fprintf(file, "#endif\n\n");
482 /******** print classname, '$' used to seperate inner-class name ***********/
484 void print_classname(classinfo *clazz)
486 utf *u = clazz->name;
487 char *endpos = u->text + u->blength;
488 char *utf_ptr = u->text;
491 while (utf_ptr < endpos) {
492 if ((c = utf_nextu2(&utf_ptr)) == '_') {
502 /*************** create table for locating native functions ****************/
504 static void printnativetableentry(methodinfo *m)
506 fprintf(file, " { \"");
507 print_classname(m->class);
508 fprintf(file, "\",\n \"");
509 utf_fprint(file, m->name);
510 fprintf(file, "\",\n \"");
511 utf_fprint(file, m->descriptor);
512 fprintf(file, "\",\n ");
514 if ((m->flags & ACC_STATIC) != 0)
515 fprintf(file, "true");
517 fprintf(file, "false");
519 fprintf(file, ",\n ");
520 fprintf(file, "(functionptr) Java_");
521 printID(m->class->name);
524 fprintf(file,"\n },\n");
528 /***************************************************************************
530 create the nativetypes-headerfile which includes
531 the headerfiles of the classes stored in the classes-chain
533 ****************************************************************************/
535 static void headers_finish()
539 char classname[1024];
541 file = fopen("nativetypes.hh", "w");
544 panic("Can not open file 'native.h' to store header information");
546 fprintf(file, "/* Headerfile for native methods: nativetypes.hh */\n");
547 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
548 fprintf(file, "\n/* include native-Headerfiles */\n\n");
550 c = chain_first(nativeclass_chain);
553 gen_header_filename(classname, c->name);
554 fprintf(file, "#include \"nat/%s.h\"\n", classname);
555 c = chain_next(nativeclass_chain);
559 chain_free(nativeclass_chain);
561 /* create table of native-methods */
563 file = fopen("nativetable.hh", "w");
566 panic("Can not open file 'nativetable' to store native-link-table");
568 fprintf(file, "/* Table of native methods: nativetables.hh */\n");
569 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
571 while ((m = chain_first(nativemethod_chain)) != NULL) {
572 chain_remove(nativemethod_chain);
573 printnativetableentry(m);
576 chain_free(nativemethod_chain);
581 /******************** internal function: print_usage ************************
583 Prints usage information for the JAVA header generator to stdout.
585 ***************************************************************************/
587 static void print_usage()
589 printf("Usage: cacaoh class [class..]\n");
594 /************************** Function: main *******************************
598 **************************************************************************/
600 int main(int argc, char **argv)
607 /********** internal (only used by main) *****************************/
609 char classpath[500] = "";
610 char offsets_filename[1024] = ""; /* phil */
611 u4 heapmaxsize = 1000000;
612 u4 heapstartsize = 100000;
614 /*********** options so only headers are generated *******************/
616 makeinitializations = false;
619 /************ Collect some info from the environment *****************/
621 cp = getenv("CLASSPATH");
623 strcpy(classpath + strlen(classpath), ":");
624 strcpy(classpath + strlen(classpath), cp);
633 /**************************** Program start **************************/
636 log_text("Java - header-generator started");
638 sprintf(offsets_filename, "jit/%s/offsets.h", ARCH_DIR);
639 file = fopen(offsets_filename, "w");
641 fprintf(stderr, "Can not open file '%s' for write", offsets_filename);
645 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
647 fprintf(file, "#define offobjvftbl %3d\n", (int) OFFSET(java_objectheader, vftbl));
648 fprintf(file, "#define offarraysize %3d\n", (int) OFFSET(java_arrayheader, size));
649 fprintf(file, "#define offobjarrdata %3d\n\n", (int) OFFSET(java_objectarray, data[0]));
650 fprintf(file, "#define offbaseval %3d\n", (int) OFFSET(vftbl, baseval));
651 fprintf(file, "#define offdiffval %3d\n\n", (int) OFFSET(vftbl, diffval));
653 fprintf(file, "#define offclassvftbl %3d\n", (int) OFFSET(classinfo, vftbl));
654 fprintf(file, "#define offclassinit %3d\n", (int) OFFSET(classinfo, initialized));
655 fprintf(file, "#define offclassloaded %3d\n", (int) OFFSET(classinfo, loaded));
656 fprintf(file, "#define offclasslinked %3d\n\n", (int) OFFSET(classinfo, linked));
658 fprintf(file, "#define offjniitemtype %3d\n", (int) OFFSET(jni_callblock, itemtype));
659 fprintf(file, "#define offjniitem %3d\n", (int) OFFSET(jni_callblock, item));
660 fprintf(file, "#define sizejniblock %3d\n\n", (int) sizeof(jni_callblock));
662 fprintf(file, "#define offclassmethodinfo %3d\n\n", (int) OFFSET(methodinfo,class));
664 fprintf(file, "#define offmethodnative %3d\n", (int) OFFSET(native_stackframeinfo,method));
665 fprintf(file, "#define offreturnfromnative %3d\n", (int) OFFSET(native_stackframeinfo,returnFromNative));
666 fprintf(file, "#define offaddrreturnfromnative %3d\n", (int) OFFSET(native_stackframeinfo,addrReturnFromNative));
667 fprintf(file, "#define offprevnative %3d\n", (int) OFFSET(native_stackframeinfo,prev));
668 fprintf(file, "#define offnextnative %3d\n", (int) OFFSET(native_stackframeinfo,next));
673 /* initialize the garbage collector */
674 gc_init(heapmaxsize, heapstartsize);
676 suck_init(classpath);
680 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
686 /*********************** Load JAVA classes **************************/
688 nativemethod_chain = chain_new();
689 nativeclass_chain = chain_new();
691 for (a = 1; a < argc; a++) {
694 /* convert classname */
695 for (i = strlen(cp) - 1; i >= 0; i--) {
703 /* topclass = loader_load(utf_new_char(cp)); */
704 topclass = class_load(class_new(utf_new_char(cp)));
705 class_link(topclass);
707 headerfile_generate(topclass);
712 /************************ Release all resources **********************/
715 tables_close(literalstring_free);
717 /* Print "finished" message */
719 log_text("Java - header-generator stopped");
727 void setVMClassField(classinfo *c)
732 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this) {
738 * These are local overrides for various environment variables in Emacs.
739 * Please do not remove this and leave it at the end of the file, where
740 * Emacs will automagically detect them.
741 * ---------------------------------------------------------------------
744 * indent-tabs-mode: t