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 1171 2004-06-12 15:04:31Z stefan $
46 #include "toolbox/chain.h"
47 #include "toolbox/memory.h"
48 #include "toolbox/logging.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 opt_stat = false;
71 bool opt_eager = false;
73 bool showmethods = false;
74 bool showconstantpool = false;
77 bool makeinitializations = true;
79 bool getloadingtime = false;
83 /******* replace some external functions *********/
85 functionptr native_findfunction(utf *cname, utf *mname, utf *desc, bool isstatic)
88 java_objectheader *javastring_new(utf *text)
91 utf *javastring_toutf(java_lang_String *string, bool isclassname)
95 /* some exception stuff */
97 char *string_java_lang_IllegalMonitorStateException =
98 "java/lang/IllegalMonitorStateException";
100 char *string_java_lang_NegativeArraySizeException =
101 "java/lang/NegativeArraySizeException";
104 char *string_java_lang_AbstractMethodError =
105 "java/lang/AbstractMethodError";
107 char *string_java_lang_ClassCircularityError =
108 "java/lang/ClassCircularityError";
110 char *string_java_lang_ClassFormatError =
111 "java/lang/ClassFormatError";
113 char *string_java_lang_ExceptionInInitializerError =
114 "java/lang/ExceptionInInitializerError";
116 char *string_java_lang_IncompatibleClassChangeError =
117 "java/lang/IncompatibleClassChangeError";
119 char *string_java_lang_InternalError =
120 "java/lang/InternalError";
122 char *string_java_lang_LinkageError =
123 "java/lang/LinkageError";
125 char *string_java_lang_NoClassDefFoundError =
126 "java/lang/NoClassDefFoundError";
128 char *string_java_lang_NoSuchFieldError =
129 "java/lang/NoSuchFieldError";
131 char *string_java_lang_NoSuchMethodError =
132 "java/lang/NoSuchMethodError";
134 char *string_java_lang_OutOfMemoryError =
135 "java/lang/OutOfMemoryError";
138 void throw_exception_exit() {}
140 void throw_cacao_exception_exit(char *exception, char *message)
143 "Exception in thread \"main\" %s: %s\n", exception, message);
151 void new_exception(char *classname) {}
152 void new_exception_message(char *classname, char *message) {}
153 void new_exception_throwable(char *classname, java_objectheader *t) {}
154 void new_exception_utfmessage(char *classname, utf *message)
156 printf("%s: ", classname);
157 utf_display(message);
162 java_objectheader *literalstring_new(utf *u) { return NULL; }
165 void literalstring_free(java_objectheader *o) { }
166 void stringtable_update() { }
167 void synchronize_caches() { }
168 void asm_call_jit_compiler() { }
169 void asm_calljavafunction() { }
170 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
172 s4 asm_builtin_idiv(s4 a, s4 b) {return 0;}
173 s4 asm_builtin_irem(s4 a, s4 b) {return 0;}
174 s8 asm_builtin_ldiv(s8 a, s8 b) {return 0;}
175 s8 asm_builtin_lrem(s8 a, s8 b) {return 0;}
177 s4 asm_builtin_f2i(float a) { return 0; }
178 s8 asm_builtin_f2l(float a) { return 0; }
179 s4 asm_builtin_d2i(double a) { return 0; }
180 s8 asm_builtin_d2l(double a) { return 0; }
182 void asm_builtin_monitorenter(java_objectheader *o) {}
183 void asm_builtin_monitorexit(java_objectheader *o) {}
185 s4 asm_builtin_checkarraycast(java_objectheader *obj, vftbl *target) {return 0;}
187 #if defined(__MIPS__)
188 void atomic_swap() {}
189 void compare_and_swap() {}
193 #if defined(__I386__)
194 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) { return 0; }
195 void asm_builtin_newarray(s4 size, vftbl *arrayvftbl) {}
198 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
200 u1 *createcompilerstub(methodinfo *m) {return NULL;}
201 u1 *createnativestub(functionptr f, methodinfo *m) {return NULL;}
202 u1 *oldcreatenativestub(functionptr f, methodinfo *m) {return NULL;}
204 void removecompilerstub(u1 *stub) {}
205 void removenativestub(u1 *stub) {}
207 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
208 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
209 void thread_restartcriticalsection() {}
210 void asm_switchstackandcall() {}
211 void asm_handle_builtin_exception(classinfo *c) {}
212 void asm_getclassvalues_atomic() {}
214 #if defined(__DARWIN__)
215 int cacao_catch_Handler() {}
218 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
219 threadcritnode asm_criticalsections;
222 java_objectheader *native_new_and_init(void *p) { return NULL; }
225 /************************ global variables **********************/
227 THREADSPECIFIC java_objectheader *_exceptionptr;
229 static chain *nativemethod_chain; /* chain with native methods */
230 static chain *nativeclass_chain; /* chain with processed classes */
231 static chain *ident_chain; /* chain with method and field names in current class */
232 static FILE *file = NULL;
233 static u4 outputsize;
234 static bool dopadding;
237 static void printIDpart(int c)
239 if ((c >= 'a' && c <= 'z')
240 || (c >= 'A' && c <= 'Z')
241 || (c >= '0' && c <= '9')
249 static void printID(utf *u)
251 char *utf_ptr = u->text;
254 for (i = 0; i < utf_strlen(u); i++)
255 printIDpart(utf_nextu2(&utf_ptr));
259 static void addoutputsize (int len)
262 if (!dopadding) return;
264 newsize = ALIGN(outputsize, len);
266 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
267 outputsize = newsize;
271 static char *printtype(char *utf_ptr)
275 switch (utf_nextu2(&utf_ptr)) {
276 case 'V': fprintf (file, "void");
282 case 'Z': addoutputsize (4);
283 fprintf (file, "s4");
285 case 'J': addoutputsize (8);
286 fprintf (file, "s8");
288 case 'F': addoutputsize (4);
289 fprintf (file, "float");
291 case 'D': addoutputsize (8);
292 fprintf (file, "double");
295 addoutputsize ( sizeof(java_arrayheader*) );
296 switch (utf_nextu2(&utf_ptr)) {
297 case 'I': fprintf (file, "java_intarray*"); break;
298 case 'J': fprintf (file, "java_longarray*"); break;
299 case 'Z': fprintf (file, "java_booleanarray*"); break;
300 case 'B': fprintf (file, "java_bytearray*"); break;
301 case 'S': fprintf (file, "java_shortarray*"); break;
302 case 'C': fprintf (file, "java_chararray*"); break;
303 case 'F': fprintf (file, "java_floatarray*"); break;
304 case 'D': fprintf (file, "java_doublearray*"); break;
306 case '[': fprintf (file, "java_objectarray*");
307 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
309 while (utf_nextu2(&utf_ptr) != ';');
312 case 'L': fprintf (file, "java_objectarray*");
313 while ( utf_nextu2(&utf_ptr) != ';');
315 default: panic ("invalid type descriptor");
320 addoutputsize ( sizeof(java_objectheader*));
321 fprintf (file, "struct ");
322 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
326 default: panic ("Unknown type in field descriptor");
333 /***** determine the number of entries of a utf string in the ident chain *****/
335 static int searchidentchain_utf(utf *ident)
337 utf *u = chain_first(ident_chain); /* first element of list */
341 if (u==ident) count++; /* string found */
342 u = chain_next(ident_chain); /* next element in list */
349 /************** print structure for direct access to objects ******************/
351 static void printfields(classinfo *c)
358 addoutputsize(sizeof(java_objectheader));
359 fprintf(file, " java_objectheader header;\n");
363 printfields(c->super);
365 for (i = 0; i < c->fieldscount; i++) {
368 if (!(f->flags & ACC_STATIC)) {
370 printtype(f->descriptor->text);
372 utf_fprint(file, f->name);
374 /* rename multiple fieldnames */
375 if ((ident_count = searchidentchain_utf(f->name)))
376 fprintf(file, "%d", ident_count - 1);
377 chain_addlast(ident_chain, f->name);
379 fprintf(file, ";\n");
385 /***************** store prototype for native method in file ******************/
387 static void printmethod(methodinfo *m)
393 /* search for return-type in descriptor */
394 utf_ptr = m->descriptor->text;
395 while (utf_nextu2(&utf_ptr) != ')');
398 fprintf(file, "\n/*\n * Class: ");
399 utf_fprint(file, m->class->name);
400 fprintf(file, "\n * Method: ");
401 utf_fprint(file, m->name);
402 fprintf(file, "\n * Signature: ");
403 utf_fprint(file, m->descriptor);
404 fprintf(file, "\n */\n");
406 /* create prototype */
407 fprintf(file, "JNIEXPORT ");
409 fprintf(file, " JNICALL Java_");
410 printID(m->class->name);
412 /* rename overloaded method */
413 if ((ident_count = searchidentchain_utf(m->name)))
414 fprintf(file, "%d", ident_count - 1);
415 chain_addlast(ident_chain, m->name);
419 fprintf(file, "(JNIEnv *env");
421 utf_ptr = m->descriptor->text + 1;
423 if (!(m->flags & ACC_STATIC)) {
424 fprintf(file, ", struct ");
425 printID(m->class->name);
426 fprintf(file, "* this");
429 fprintf(file, ", jclass clazz");
432 if ((*utf_ptr) != ')') fprintf(file, ", ");
434 while ((*utf_ptr) != ')') {
435 utf_ptr = printtype(utf_ptr);
436 fprintf(file, " par%d", paramnum++);
437 if ((*utf_ptr)!=')') fprintf(file, ", ");
440 fprintf(file, ");\n\n");
444 /******* remove package-name in fully-qualified classname *********************/
446 static void gen_header_filename(char *buffer, utf *u)
450 for (i = 0;i < utf_strlen(u); i++) {
451 if ((u->text[i] == '/') || (u->text[i] == '$')) buffer[i] = '_'; /* convert '$' and '/' to '_' */
452 else buffer[i] = u->text[i];
454 buffer[utf_strlen(u)] = '\0';
458 /* create headerfile for classes and store native methods in chain ************/
460 static void headerfile_generate(classinfo *c)
462 char header_filename[1024] = "";
463 char classname[1024];
464 char uclassname[1024];
468 /* store class in chain */
469 chain_addlast(nativeclass_chain, c);
471 /* open headerfile for class */
472 gen_header_filename(classname, c->name);
474 /* create chain for renaming fields */
475 ident_chain = chain_new();
477 sprintf(header_filename, "nat/%s.h", classname);
478 file = fopen(header_filename, "w");
479 if (!file) panic("Can not open file to store header information");
481 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
483 /* convert to uppercase */
484 for (i = 0; classname[i]; i++) {
485 uclassname[i] = toupper(classname[i]);
487 uclassname[i] = '\0';
489 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
491 /* create structure for direct access to objects */
492 fprintf(file, "/* Structure information for class: ");
493 utf_fprint(file, c->name);
494 fprintf(file, " */\n\n");
495 fprintf(file, "typedef struct ");
497 fprintf(file, " {\n");
505 fprintf(file, ";\n\n");
507 /* create chain for renaming overloaded methods */
508 chain_free(ident_chain);
509 ident_chain = chain_new();
511 /* create method-prototypes */
513 for (i = 0; i < c->methodscount; i++) {
515 m = &(c->methods[i]);
517 if (m->flags & ACC_NATIVE) {
518 chain_addlast(nativemethod_chain, m);
523 chain_free(ident_chain);
525 fprintf(file, "#endif\n\n");
531 /******** print classname, '$' used to seperate inner-class name ***********/
533 void print_classname(classinfo *clazz)
535 utf *u = clazz->name;
536 char *endpos = u->text + u->blength;
537 char *utf_ptr = u->text;
540 while (utf_ptr < endpos) {
541 if ((c = utf_nextu2(&utf_ptr)) == '_') {
551 /*************** create table for locating native functions ****************/
553 static void printnativetableentry(methodinfo *m)
555 fprintf(file, " { \"");
556 print_classname(m->class);
557 fprintf(file, "\",\n \"");
558 utf_fprint(file, m->name);
559 fprintf(file, "\",\n \"");
560 utf_fprint(file, m->descriptor);
561 fprintf(file, "\",\n ");
563 if ((m->flags & ACC_STATIC) != 0)
564 fprintf(file, "true");
566 fprintf(file, "false");
568 fprintf(file, ",\n ");
569 fprintf(file, "(functionptr) Java_");
570 printID(m->class->name);
573 fprintf(file,"\n },\n");
577 /***************************************************************************
579 create the nativetypes-headerfile which includes
580 the headerfiles of the classes stored in the classes-chain
582 ****************************************************************************/
584 static void headers_finish()
588 char classname[1024];
590 file = fopen("nativetypes.hh", "w");
593 panic("Can not open file 'native.h' to store header information");
595 fprintf(file, "/* Headerfile for native methods: nativetypes.hh */\n");
596 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
597 fprintf(file, "\n/* include native-Headerfiles */\n\n");
599 c = chain_first(nativeclass_chain);
602 gen_header_filename(classname, c->name);
603 fprintf(file, "#include \"nat/%s.h\"\n", classname);
604 c = chain_next(nativeclass_chain);
608 chain_free(nativeclass_chain);
610 /* create table of native-methods */
612 file = fopen("nativetable.hh", "w");
615 panic("Can not open file 'nativetable' to store native-link-table");
617 fprintf(file, "/* Table of native methods: nativetables.hh */\n");
618 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
620 while ((m = chain_first(nativemethod_chain)) != NULL) {
621 chain_remove(nativemethod_chain);
622 printnativetableentry(m);
625 chain_free(nativemethod_chain);
630 /******************** internal function: print_usage ************************
632 Prints usage information for the JAVA header generator to stdout.
634 ***************************************************************************/
636 static void print_usage()
638 printf("Usage: cacaoh class [class..]\n");
643 /************************** Function: main *******************************
647 **************************************************************************/
649 int main(int argc, char **argv)
656 /********** internal (only used by main) *****************************/
658 char classpath[500] = "";
659 char offsets_filename[1024] = ""; /* phil */
660 u4 heapmaxsize = 1000000;
661 u4 heapstartsize = 100000;
663 /*********** options so only headers are generated *******************/
665 makeinitializations = false;
668 /************ Collect some info from the environment *****************/
670 cp = getenv("CLASSPATH");
672 strcpy(classpath + strlen(classpath), ":");
673 strcpy(classpath + strlen(classpath), cp);
682 /**************************** Program start **************************/
685 log_text("Java - header-generator started");
687 sprintf(offsets_filename, "jit/%s/offsets.h", ARCH_DIR);
688 file = fopen(offsets_filename, "w");
690 fprintf(stderr, "Can not open file '%s' for write", offsets_filename);
694 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
696 fprintf(file, "#define offobjvftbl %3d\n", (int) OFFSET(java_objectheader, vftbl));
697 fprintf(file, "#define offarraysize %3d\n", (int) OFFSET(java_arrayheader, size));
698 fprintf(file, "#define offobjarrdata %3d\n\n", (int) OFFSET(java_objectarray, data[0]));
699 fprintf(file, "#define offbaseval %3d\n", (int) OFFSET(vftbl, baseval));
700 fprintf(file, "#define offdiffval %3d\n\n", (int) OFFSET(vftbl, diffval));
701 fprintf(file, "#define offclass %3d\n\n", (int) OFFSET(vftbl, class));
703 fprintf(file, "#define offclassvftbl %3d\n", (int) OFFSET(classinfo, vftbl));
704 fprintf(file, "#define offclassinit %3d\n", (int) OFFSET(classinfo, initialized));
705 fprintf(file, "#define offclassloaded %3d\n", (int) OFFSET(classinfo, loaded));
706 fprintf(file, "#define offclasslinked %3d\n\n", (int) OFFSET(classinfo, linked));
708 fprintf(file, "#define offjniitemtype %3d\n", (int) OFFSET(jni_callblock, itemtype));
709 fprintf(file, "#define offjniitem %3d\n", (int) OFFSET(jni_callblock, item));
710 fprintf(file, "#define sizejniblock %3d\n\n", (int) sizeof(jni_callblock));
712 fprintf(file, "#define offclassmethodinfo %3d\n\n", (int) OFFSET(methodinfo,class));
714 fprintf(file, "#define offmethodnative %3d\n", (int) OFFSET(native_stackframeinfo,method));
715 /* fprintf(file, "#define offreturnfromnative %3d\n", (int) OFFSET(native_stackframeinfo,returnFromNative));
716 fprintf(file, "#define offaddrreturnfromnative %3d\n", (int) OFFSET(native_stackframeinfo,addrReturnFromNative));
717 fprintf(file, "#define offprevnative %3d\n", (int) OFFSET(native_stackframeinfo,prev));
718 fprintf(file, "#define offnextnative %3d\n", (int) OFFSET(native_stackframeinfo,next));*/
721 fprintf(file, "#define offcast_super_baseval %3d\n", (int) OFFSET(castinfo, super_baseval));
722 fprintf(file, "#define offcast_super_diffval %3d\n", (int) OFFSET(castinfo, super_diffval));
723 fprintf(file, "#define offcast_sub_baseval %3d\n", (int) OFFSET(castinfo, sub_baseval));
727 /* initialize the garbage collector */
728 gc_init(heapmaxsize, heapstartsize);
730 suck_init(classpath);
734 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
740 /*********************** Load JAVA classes **************************/
742 nativemethod_chain = chain_new();
743 nativeclass_chain = chain_new();
745 for (a = 1; a < argc; a++) {
748 /* convert classname */
749 for (i = strlen(cp) - 1; i >= 0; i--) {
757 /* topclass = loader_load(utf_new_char(cp)); */
758 topclass = class_load(class_new(utf_new_char(cp)));
759 class_link(topclass);
761 headerfile_generate(topclass);
766 /************************ Release all resources **********************/
769 tables_close(literalstring_free);
771 /* Print "finished" message */
773 log_text("Java - header-generator stopped");
781 void setVMClassField(classinfo *c)
786 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this) {
792 * These are local overrides for various environment variables in Emacs.
793 * Please do not remove this and leave it at the end of the file, where
794 * Emacs will automagically detect them.
795 * ---------------------------------------------------------------------
798 * indent-tabs-mode: t