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 1082 2004-05-26 15:04:54Z jowenn $
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 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 thread_restartcriticalsection() {}
171 void asm_switchstackandcall() {}
172 void asm_handle_builtin_exception(classinfo *c) {}
173 void asm_getclassvalues_atomic() {}
175 #if defined(__DARWIN__)
176 int cacao_catch_Handler() {}
179 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
180 threadcritnode asm_criticalsections;
183 java_objectheader *native_new_and_init(void *p) { return NULL; }
186 /************************ global variables **********************/
188 THREADSPECIFIC java_objectheader *_exceptionptr;
190 static chain *nativemethod_chain; /* chain with native methods */
191 static chain *nativeclass_chain; /* chain with processed classes */
192 static chain *ident_chain; /* chain with method and field names in current class */
193 static FILE *file = NULL;
194 static u4 outputsize;
195 static bool dopadding;
198 static void printIDpart(int c)
200 if ((c >= 'a' && c <= 'z')
201 || (c >= 'A' && c <= 'Z')
202 || (c >= '0' && c <= '9')
210 static void printID(utf *u)
212 char *utf_ptr = u->text;
215 for (i = 0; i < utf_strlen(u); i++)
216 printIDpart(utf_nextu2(&utf_ptr));
220 static void addoutputsize (int len)
223 if (!dopadding) return;
225 newsize = ALIGN(outputsize, len);
227 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
228 outputsize = newsize;
232 static char *printtype(char *utf_ptr)
236 switch (utf_nextu2(&utf_ptr)) {
237 case 'V': fprintf (file, "void");
243 case 'Z': addoutputsize (4);
244 fprintf (file, "s4");
246 case 'J': addoutputsize (8);
247 fprintf (file, "s8");
249 case 'F': addoutputsize (4);
250 fprintf (file, "float");
252 case 'D': addoutputsize (8);
253 fprintf (file, "double");
256 addoutputsize ( sizeof(java_arrayheader*) );
257 switch (utf_nextu2(&utf_ptr)) {
258 case 'I': fprintf (file, "java_intarray*"); break;
259 case 'J': fprintf (file, "java_longarray*"); break;
260 case 'Z': fprintf (file, "java_booleanarray*"); break;
261 case 'B': fprintf (file, "java_bytearray*"); break;
262 case 'S': fprintf (file, "java_shortarray*"); break;
263 case 'C': fprintf (file, "java_chararray*"); break;
264 case 'F': fprintf (file, "java_floatarray*"); break;
265 case 'D': fprintf (file, "java_doublearray*"); break;
267 case '[': fprintf (file, "java_objectarray*");
268 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
270 while (utf_nextu2(&utf_ptr) != ';');
273 case 'L': fprintf (file, "java_objectarray*");
274 while ( utf_nextu2(&utf_ptr) != ';');
276 default: panic ("invalid type descriptor");
281 addoutputsize ( sizeof(java_objectheader*));
282 fprintf (file, "struct ");
283 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
287 default: panic ("Unknown type in field descriptor");
294 /***** determine the number of entries of a utf string in the ident chain *****/
296 static int searchidentchain_utf(utf *ident)
298 utf *u = chain_first(ident_chain); /* first element of list */
302 if (u==ident) count++; /* string found */
303 u = chain_next(ident_chain); /* next element in list */
310 /************** print structure for direct access to objects ******************/
312 static void printfields(classinfo *c)
319 addoutputsize(sizeof(java_objectheader));
320 fprintf(file, " java_objectheader header;\n");
324 printfields(c->super);
326 for (i = 0; i < c->fieldscount; i++) {
329 if (!(f->flags & ACC_STATIC)) {
331 printtype(f->descriptor->text);
333 utf_fprint(file, f->name);
335 /* rename multiple fieldnames */
336 if ((ident_count = searchidentchain_utf(f->name)))
337 fprintf(file, "%d", ident_count - 1);
338 chain_addlast(ident_chain, f->name);
340 fprintf(file, ";\n");
346 /***************** store prototype for native method in file ******************/
348 static void printmethod(methodinfo *m)
354 /* search for return-type in descriptor */
355 utf_ptr = m->descriptor->text;
356 while (utf_nextu2(&utf_ptr) != ')');
359 fprintf(file, "\n/*\n * Class: ");
360 utf_fprint(file, m->class->name);
361 fprintf(file, "\n * Method: ");
362 utf_fprint(file, m->name);
363 fprintf(file, "\n * Signature: ");
364 utf_fprint(file, m->descriptor);
365 fprintf(file, "\n */\n");
367 /* create prototype */
368 fprintf(file, "JNIEXPORT ");
370 fprintf(file, " JNICALL Java_");
371 printID(m->class->name);
373 /* rename overloaded method */
374 if ((ident_count = searchidentchain_utf(m->name)))
375 fprintf(file, "%d", ident_count - 1);
376 chain_addlast(ident_chain, m->name);
380 fprintf(file, "(JNIEnv *env");
382 utf_ptr = m->descriptor->text + 1;
384 if (!(m->flags & ACC_STATIC)) {
385 fprintf(file, ", struct ");
386 printID(m->class->name);
387 fprintf(file, "* this");
390 fprintf(file, ", jclass clazz");
393 if ((*utf_ptr) != ')') fprintf(file, ", ");
395 while ((*utf_ptr) != ')') {
396 utf_ptr = printtype(utf_ptr);
397 fprintf(file, " par%d", paramnum++);
398 if ((*utf_ptr)!=')') fprintf(file, ", ");
401 fprintf(file, ");\n\n");
405 /******* remove package-name in fully-qualified classname *********************/
407 static void gen_header_filename(char *buffer, utf *u)
411 for (i = 0;i < utf_strlen(u); i++) {
412 if ((u->text[i] == '/') || (u->text[i] == '$')) buffer[i] = '_'; /* convert '$' and '/' to '_' */
413 else buffer[i] = u->text[i];
415 buffer[utf_strlen(u)] = '\0';
419 /* create headerfile for classes and store native methods in chain ************/
421 static void headerfile_generate(classinfo *c)
423 char header_filename[1024] = "";
424 char classname[1024];
425 char uclassname[1024];
429 /* store class in chain */
430 chain_addlast(nativeclass_chain, c);
432 /* open headerfile for class */
433 gen_header_filename(classname, c->name);
435 /* create chain for renaming fields */
436 ident_chain = chain_new();
438 sprintf(header_filename, "nat/%s.h", classname);
439 file = fopen(header_filename, "w");
440 if (!file) panic("Can not open file to store header information");
442 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
444 /* convert to uppercase */
445 for (i = 0; classname[i]; i++) {
446 uclassname[i] = toupper(classname[i]);
448 uclassname[i] = '\0';
450 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
452 /* create structure for direct access to objects */
453 fprintf(file, "/* Structure information for class: ");
454 utf_fprint(file, c->name);
455 fprintf(file, " */\n\n");
456 fprintf(file, "typedef struct ");
458 fprintf(file, " {\n");
466 fprintf(file, ";\n\n");
468 /* create chain for renaming overloaded methods */
469 chain_free(ident_chain);
470 ident_chain = chain_new();
472 /* create method-prototypes */
474 for (i = 0; i < c->methodscount; i++) {
476 m = &(c->methods[i]);
478 if (m->flags & ACC_NATIVE) {
479 chain_addlast(nativemethod_chain, m);
484 chain_free(ident_chain);
486 fprintf(file, "#endif\n\n");
492 /******** print classname, '$' used to seperate inner-class name ***********/
494 void print_classname(classinfo *clazz)
496 utf *u = clazz->name;
497 char *endpos = u->text + u->blength;
498 char *utf_ptr = u->text;
501 while (utf_ptr < endpos) {
502 if ((c = utf_nextu2(&utf_ptr)) == '_') {
512 /*************** create table for locating native functions ****************/
514 static void printnativetableentry(methodinfo *m)
516 fprintf(file, " { \"");
517 print_classname(m->class);
518 fprintf(file, "\",\n \"");
519 utf_fprint(file, m->name);
520 fprintf(file, "\",\n \"");
521 utf_fprint(file, m->descriptor);
522 fprintf(file, "\",\n ");
524 if ((m->flags & ACC_STATIC) != 0)
525 fprintf(file, "true");
527 fprintf(file, "false");
529 fprintf(file, ",\n ");
530 fprintf(file, "(functionptr) Java_");
531 printID(m->class->name);
534 fprintf(file,"\n },\n");
538 /***************************************************************************
540 create the nativetypes-headerfile which includes
541 the headerfiles of the classes stored in the classes-chain
543 ****************************************************************************/
545 static void headers_finish()
549 char classname[1024];
551 file = fopen("nativetypes.hh", "w");
554 panic("Can not open file 'native.h' to store header information");
556 fprintf(file, "/* Headerfile for native methods: nativetypes.hh */\n");
557 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
558 fprintf(file, "\n/* include native-Headerfiles */\n\n");
560 c = chain_first(nativeclass_chain);
563 gen_header_filename(classname, c->name);
564 fprintf(file, "#include \"nat/%s.h\"\n", classname);
565 c = chain_next(nativeclass_chain);
569 chain_free(nativeclass_chain);
571 /* create table of native-methods */
573 file = fopen("nativetable.hh", "w");
576 panic("Can not open file 'nativetable' to store native-link-table");
578 fprintf(file, "/* Table of native methods: nativetables.hh */\n");
579 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
581 while ((m = chain_first(nativemethod_chain)) != NULL) {
582 chain_remove(nativemethod_chain);
583 printnativetableentry(m);
586 chain_free(nativemethod_chain);
591 /******************** internal function: print_usage ************************
593 Prints usage information for the JAVA header generator to stdout.
595 ***************************************************************************/
597 static void print_usage()
599 printf("Usage: cacaoh class [class..]\n");
604 /************************** Function: main *******************************
608 **************************************************************************/
610 int main(int argc, char **argv)
617 /********** internal (only used by main) *****************************/
619 char classpath[500] = "";
620 char offsets_filename[1024] = ""; /* phil */
621 u4 heapmaxsize = 1000000;
622 u4 heapstartsize = 100000;
624 /*********** options so only headers are generated *******************/
626 makeinitializations = false;
629 /************ Collect some info from the environment *****************/
631 cp = getenv("CLASSPATH");
633 strcpy(classpath + strlen(classpath), ":");
634 strcpy(classpath + strlen(classpath), cp);
643 /**************************** Program start **************************/
646 log_text("Java - header-generator started");
648 sprintf(offsets_filename, "jit/%s/offsets.h", ARCH_DIR);
649 file = fopen(offsets_filename, "w");
651 fprintf(stderr, "Can not open file '%s' for write", offsets_filename);
655 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
657 fprintf(file, "#define offobjvftbl %3d\n", (int) OFFSET(java_objectheader, vftbl));
658 fprintf(file, "#define offarraysize %3d\n", (int) OFFSET(java_arrayheader, size));
659 fprintf(file, "#define offobjarrdata %3d\n\n", (int) OFFSET(java_objectarray, data[0]));
660 fprintf(file, "#define offbaseval %3d\n", (int) OFFSET(vftbl, baseval));
661 fprintf(file, "#define offdiffval %3d\n\n", (int) OFFSET(vftbl, diffval));
663 fprintf(file, "#define offclassvftbl %3d\n", (int) OFFSET(classinfo, vftbl));
664 fprintf(file, "#define offclassinit %3d\n", (int) OFFSET(classinfo, initialized));
665 fprintf(file, "#define offclassloaded %3d\n", (int) OFFSET(classinfo, loaded));
666 fprintf(file, "#define offclasslinked %3d\n\n", (int) OFFSET(classinfo, linked));
668 fprintf(file, "#define offjniitemtype %3d\n", (int) OFFSET(jni_callblock, itemtype));
669 fprintf(file, "#define offjniitem %3d\n", (int) OFFSET(jni_callblock, item));
670 fprintf(file, "#define sizejniblock %3d\n\n", (int) sizeof(jni_callblock));
672 fprintf(file, "#define offclassmethodinfo %3d\n\n", (int) OFFSET(methodinfo,class));
674 fprintf(file, "#define offmethodnative %3d\n", (int) OFFSET(native_stackframeinfo,method));
675 /* fprintf(file, "#define offreturnfromnative %3d\n", (int) OFFSET(native_stackframeinfo,returnFromNative));
676 fprintf(file, "#define offaddrreturnfromnative %3d\n", (int) OFFSET(native_stackframeinfo,addrReturnFromNative));
677 fprintf(file, "#define offprevnative %3d\n", (int) OFFSET(native_stackframeinfo,prev));
678 fprintf(file, "#define offnextnative %3d\n", (int) OFFSET(native_stackframeinfo,next));*/
681 fprintf(file, "#define offcast_super_baseval %3d\n", (int) OFFSET(castinfo, super_baseval));
682 fprintf(file, "#define offcast_super_diffval %3d\n", (int) OFFSET(castinfo, super_diffval));
683 fprintf(file, "#define offcast_sub_baseval %3d\n", (int) OFFSET(castinfo, sub_baseval));
687 /* initialize the garbage collector */
688 gc_init(heapmaxsize, heapstartsize);
690 suck_init(classpath);
694 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
700 /*********************** Load JAVA classes **************************/
702 nativemethod_chain = chain_new();
703 nativeclass_chain = chain_new();
705 for (a = 1; a < argc; a++) {
708 /* convert classname */
709 for (i = strlen(cp) - 1; i >= 0; i--) {
717 /* topclass = loader_load(utf_new_char(cp)); */
718 topclass = class_load(class_new(utf_new_char(cp)));
719 class_link(topclass);
721 headerfile_generate(topclass);
726 /************************ Release all resources **********************/
729 tables_close(literalstring_free);
731 /* Print "finished" message */
733 log_text("Java - header-generator stopped");
741 void setVMClassField(classinfo *c)
746 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this) {
752 * These are local overrides for various environment variables in Emacs.
753 * Please do not remove this and leave it at the end of the file, where
754 * Emacs will automagically detect them.
755 * ---------------------------------------------------------------------
758 * indent-tabs-mode: t