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 960 2004-03-14 23:41:11Z 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 compileall = false;
56 bool runverbose = false;
57 bool collectverbose = false;
59 bool loadverbose = false;
60 bool linkverbose = false;
61 bool initverbose = false;
63 bool opt_rt = false; /* true if RTA parse should be used RT-CO */
64 bool opt_xta = false; /* true if XTA parse should be used XTA-CO */
65 bool opt_vta = false; /* true if VTA parse should be used VTA-CO */
66 bool opt_verify = true; /* true if classfiles should be verified */
67 bool opt_liberalutf = false; /* Don't check overlong UTF-8 sequences */
69 bool showmethods = false;
70 bool showconstantpool = false;
73 bool makeinitializations = true;
75 bool getloadingtime = false;
79 /******* replace some external functions *********/
81 functionptr native_findfunction(utf *cname, utf *mname, utf *desc, bool isstatic)
84 java_objectheader *javastring_new(utf *text)
87 utf *javastring_toutf(java_lang_String *string, bool isclassname)
90 void throw_noclassdeffounderror_message(utf* classname)
92 printf("Class not found: ");
93 utf_display(classname);
99 /* this is here to avoid link errors.
100 We are not interested in linkagerrors in cacaoh right now
102 void throw_linkageerror_message(utf* classname) {}
104 void throw_exception() {}
105 void new_exception(char *classname) {}
106 void new_exception_message(char *classname, char *message) {}
108 java_objectheader *literalstring_new(utf *u) { return NULL; }
111 void literalstring_free(java_objectheader *o) { }
112 void stringtable_update() { }
113 void synchronize_caches() { }
114 void asm_call_jit_compiler() { }
115 void asm_calljavafunction() { }
116 void asm_dumpregistersandcall() { }
117 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
119 s4 asm_builtin_idiv(s4 a, s4 b) {return 0;}
120 s4 asm_builtin_irem(s4 a, s4 b) {return 0;}
121 s8 asm_builtin_ldiv(s8 a, s8 b) {return 0;}
122 s8 asm_builtin_lrem(s8 a, s8 b) {return 0;}
124 s4 asm_builtin_f2i(float a) { return 0; }
125 s8 asm_builtin_f2l(float a) { return 0; }
126 s4 asm_builtin_d2i(double a) { return 0; }
127 s8 asm_builtin_d2l(double a) { return 0; }
129 void asm_builtin_monitorenter(java_objectheader *o) {}
130 void asm_builtin_monitorexit(java_objectheader *o) {}
132 s4 asm_builtin_checkarraycast(java_objectheader *obj, vftbl *target) {return 0;}
135 #if defined(__I386__)
136 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) { return 0; }
137 void asm_builtin_newarray(s4 size, vftbl *arrayvftbl) {}
140 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
142 u1 *createcompilerstub(methodinfo *m) {return NULL;}
143 u1 *createnativestub(functionptr f, methodinfo *m) {return NULL;}
144 u1 *oldcreatenativestub(functionptr f, methodinfo *m) {return NULL;}
146 void removecompilerstub(u1 *stub) {}
147 void removenativestub(u1 *stub) {}
149 void asm_perform_threadswitch(u1 **from, u1 **to) {}
150 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
151 void asm_switchstackandcall() {}
152 void asm_handle_builtin_exception(classinfo *c) {}
154 java_objectheader *native_new_and_init(void *p) { return NULL; }
157 /************************ global variables **********************/
159 THREADSPECIFIC java_objectheader *_exceptionptr;
161 static chain *nativemethod_chain; /* chain with native methods */
162 static chain *nativeclass_chain; /* chain with processed classes */
163 static chain *ident_chain; /* chain with method and field names in current class */
164 static FILE *file = NULL;
165 static u4 outputsize;
166 static bool dopadding;
169 static void printIDpart(int c)
171 if ((c >= 'a' && c <= 'z')
172 || (c >= 'A' && c <= 'Z')
173 || (c >= '0' && c <= '9')
181 static void printID(utf *u)
183 char *utf_ptr = u->text;
186 for (i = 0; i < utf_strlen(u); i++)
187 printIDpart(utf_nextu2(&utf_ptr));
191 static void addoutputsize (int len)
194 if (!dopadding) return;
196 newsize = ALIGN(outputsize, len);
198 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
199 outputsize = newsize;
203 static char *printtype(char *utf_ptr)
207 switch (utf_nextu2(&utf_ptr)) {
208 case 'V': fprintf (file, "void");
214 case 'Z': addoutputsize (4);
215 fprintf (file, "s4");
217 case 'J': addoutputsize (8);
218 fprintf (file, "s8");
220 case 'F': addoutputsize (4);
221 fprintf (file, "float");
223 case 'D': addoutputsize (8);
224 fprintf (file, "double");
227 addoutputsize ( sizeof(java_arrayheader*) );
228 switch (utf_nextu2(&utf_ptr)) {
229 case 'I': fprintf (file, "java_intarray*"); break;
230 case 'J': fprintf (file, "java_longarray*"); break;
231 case 'Z': fprintf (file, "java_booleanarray*"); break;
232 case 'B': fprintf (file, "java_bytearray*"); break;
233 case 'S': fprintf (file, "java_shortarray*"); break;
234 case 'C': fprintf (file, "java_chararray*"); break;
235 case 'F': fprintf (file, "java_floatarray*"); break;
236 case 'D': fprintf (file, "java_doublearray*"); break;
238 case '[': fprintf (file, "java_objectarray*");
239 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
241 while (utf_nextu2(&utf_ptr) != ';');
244 case 'L': fprintf (file, "java_objectarray*");
245 while ( utf_nextu2(&utf_ptr) != ';');
247 default: panic ("invalid type descriptor");
252 addoutputsize ( sizeof(java_objectheader*));
253 fprintf (file, "struct ");
254 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
258 default: panic ("Unknown type in field descriptor");
265 /***** determine the number of entries of a utf string in the ident chain *****/
267 static int searchidentchain_utf(utf *ident)
269 utf *u = chain_first(ident_chain); /* first element of list */
273 if (u==ident) count++; /* string found */
274 u = chain_next(ident_chain); /* next element in list */
281 /************** print structure for direct access to objects ******************/
283 static void printfields(classinfo *c)
290 addoutputsize(sizeof(java_objectheader));
291 fprintf(file, " java_objectheader header;\n");
295 printfields(c->super);
297 for (i = 0; i < c->fieldscount; i++) {
300 if (!(f->flags & ACC_STATIC)) {
302 printtype(f->descriptor->text);
304 utf_fprint(file, f->name);
306 /* rename multiple fieldnames */
307 if ((ident_count = searchidentchain_utf(f->name)))
308 fprintf(file, "%d", ident_count - 1);
309 chain_addlast(ident_chain, f->name);
311 fprintf(file, ";\n");
317 /***************** store prototype for native method in file ******************/
319 static void printmethod(methodinfo *m)
325 /* search for return-type in descriptor */
326 utf_ptr = m->descriptor->text;
327 while (utf_nextu2(&utf_ptr) != ')');
330 fprintf(file, "\n/*\n * Class: ");
331 utf_fprint(file, m->class->name);
332 fprintf(file, "\n * Method: ");
333 utf_fprint(file, m->name);
334 fprintf(file, "\n * Signature: ");
335 utf_fprint(file, m->descriptor);
336 fprintf(file, "\n */\n");
338 /* create prototype */
339 fprintf(file, "JNIEXPORT ");
341 fprintf(file, " JNICALL Java_");
342 printID(m->class->name);
344 /* rename overloaded method */
345 if ((ident_count = searchidentchain_utf(m->name)))
346 fprintf(file, "%d", ident_count - 1);
347 chain_addlast(ident_chain, m->name);
351 fprintf(file, "(JNIEnv *env");
353 utf_ptr = m->descriptor->text + 1;
355 if (!(m->flags & ACC_STATIC)) {
356 fprintf(file, ", struct ");
357 printID(m->class->name);
358 fprintf(file, "* this");
361 fprintf(file, ", jclass clazz");
364 if ((*utf_ptr) != ')') fprintf(file, ", ");
366 while ((*utf_ptr) != ')') {
367 utf_ptr = printtype(utf_ptr);
368 fprintf(file, " par%d", paramnum++);
369 if ((*utf_ptr)!=')') fprintf(file, ", ");
372 fprintf(file, ");\n\n");
376 /******* remove package-name in fully-qualified classname *********************/
378 static void gen_header_filename(char *buffer, utf *u)
382 for (i = 0;i < utf_strlen(u); i++) {
383 if ((u->text[i] == '/') || (u->text[i] == '$')) buffer[i] = '_'; /* convert '$' and '/' to '_' */
384 else buffer[i] = u->text[i];
386 buffer[utf_strlen(u)] = '\0';
390 /* create headerfile for classes and store native methods in chain ************/
392 static void headerfile_generate(classinfo *c)
394 char header_filename[1024] = "";
395 char classname[1024];
396 char uclassname[1024];
400 /* store class in chain */
401 chain_addlast(nativeclass_chain, c);
403 /* open headerfile for class */
404 gen_header_filename(classname, c->name);
406 /* create chain for renaming fields */
407 ident_chain = chain_new();
409 sprintf(header_filename, "nat/%s.h", classname);
410 file = fopen(header_filename, "w");
411 if (!file) panic("Can not open file to store header information");
413 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
415 /* convert to uppercase */
416 for (i = 0; classname[i]; i++) {
417 uclassname[i] = toupper(classname[i]);
419 uclassname[i] = '\0';
421 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
423 /* create structure for direct access to objects */
424 fprintf(file, "/* Structure information for class: ");
425 utf_fprint(file, c->name);
426 fprintf(file, " */\n\n");
427 fprintf(file, "typedef struct ");
429 fprintf(file, " {\n");
435 fprintf(file, ";\n\n");
437 /* create chain for renaming overloaded methods */
438 chain_free(ident_chain);
439 ident_chain = chain_new();
441 /* create method-prototypes */
443 for (i = 0; i < c->methodscount; i++) {
445 m = &(c->methods[i]);
447 if (m->flags & ACC_NATIVE) {
448 chain_addlast(nativemethod_chain, m);
453 chain_free(ident_chain);
455 fprintf(file, "#endif\n\n");
461 /******** print classname, '$' used to seperate inner-class name ***********/
463 void print_classname(classinfo *clazz)
465 utf *u = clazz->name;
466 char *endpos = u->text + u->blength;
467 char *utf_ptr = u->text;
470 while (utf_ptr < endpos) {
471 if ((c = utf_nextu2(&utf_ptr)) == '_') {
481 /*************** create table for locating native functions ****************/
483 static void printnativetableentry(methodinfo *m)
485 fprintf(file, " { \"");
486 print_classname(m->class);
487 fprintf(file, "\",\n \"");
488 utf_fprint(file, m->name);
489 fprintf(file, "\",\n \"");
490 utf_fprint(file, m->descriptor);
491 fprintf(file, "\",\n ");
493 if ((m->flags & ACC_STATIC) != 0)
494 fprintf(file, "true");
496 fprintf(file, "false");
498 fprintf(file, ",\n ");
499 fprintf(file, "(functionptr) Java_");
500 printID(m->class->name);
503 fprintf(file,"\n },\n");
507 /***************************************************************************
509 create the nativetypes-headerfile which includes
510 the headerfiles of the classes stored in the classes-chain
512 ****************************************************************************/
514 static void headers_finish()
518 char classname[1024];
520 file = fopen("nativetypes.hh", "w");
523 panic("Can not open file 'native.h' to store header information");
525 fprintf(file, "/* Headerfile for native methods: nativetypes.hh */\n");
526 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
527 fprintf(file, "\n/* include native-Headerfiles */\n\n");
529 c = chain_first(nativeclass_chain);
532 gen_header_filename(classname, c->name);
533 fprintf(file, "#include \"nat/%s.h\"\n", classname);
534 c = chain_next(nativeclass_chain);
538 chain_free(nativeclass_chain);
540 /* create table of native-methods */
542 file = fopen("nativetable.hh", "w");
545 panic("Can not open file 'nativetable' to store native-link-table");
547 fprintf(file, "/* Table of native methods: nativetables.hh */\n");
548 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
550 while ((m = chain_first(nativemethod_chain)) != NULL) {
551 chain_remove(nativemethod_chain);
552 printnativetableentry(m);
555 chain_free(nativemethod_chain);
560 /******************** internal function: print_usage ************************
562 Prints usage information for the JAVA header generator to stdout.
564 ***************************************************************************/
566 static void print_usage()
568 printf("Usage: cacaoh class [class..]\n");
573 /************************** Function: main *******************************
577 **************************************************************************/
579 int main(int argc, char **argv)
587 /********** internal (only used by main) *****************************/
589 char classpath[500] = "";
590 char offsets_filename[1024] = ""; /* phil */
591 u4 heapsize = 100000;
593 /*********** options so only headers are generated *******************/
595 makeinitializations = false;
598 /************ Collect some info from the environment *****************/
600 cp = getenv("CLASSPATH");
602 strcpy(classpath + strlen(classpath), ":");
603 strcpy(classpath + strlen(classpath), cp);
612 /**************************** Program start **************************/
615 log_text("Java - header-generator started");
617 sprintf(offsets_filename, "jit/%s/offsets.h", ARCH_DIR);
618 file = fopen(offsets_filename, "w");
620 fprintf(stderr, "Can not open file '%s' for write", offsets_filename);
624 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
626 fprintf(file, "#define offobjvftbl %3d\n", (int) OFFSET(java_objectheader, vftbl));
627 fprintf(file, "#define offarraysize %3d\n", (int) OFFSET(java_arrayheader, size));
628 fprintf(file, "#define offobjarrdata %3d\n\n", (int) OFFSET(java_objectarray, data[0]));
629 fprintf(file, "#define offbaseval %3d\n", (int) OFFSET(vftbl, baseval));
630 fprintf(file, "#define offdiffval %3d\n\n", (int) OFFSET(vftbl, diffval));
632 fprintf(file, "#define offclassvftbl %3d\n", (int) OFFSET(classinfo, vftbl));
633 fprintf(file, "#define offclassinit %3d\n\n", (int) OFFSET(classinfo, initialized));
635 fprintf(file, "#define offjniitemtype %3d\n", (int) OFFSET(jni_callblock, itemtype));
636 fprintf(file, "#define offjniitem %3d\n", (int) OFFSET(jni_callblock, item));
637 fprintf(file, "#define sizejniblock %3d\n\n", (int) sizeof(jni_callblock));
639 fprintf(file, "#define offclassmethodinfo %3d\n\n", (int) OFFSET(methodinfo,class));
641 fprintf(file, "#define offmethodnative %3d\n", (int) OFFSET(native_stackframeinfo,method));
642 fprintf(file, "#define offreturnfromnative %3d\n", (int) OFFSET(native_stackframeinfo,returnFromNative));
643 fprintf(file, "#define offaddrreturnfromnative %3d\n", (int) OFFSET(native_stackframeinfo,addrReturnFromNative));
644 fprintf(file, "#define offprevnative %3d\n", (int) OFFSET(native_stackframeinfo,prev));
645 fprintf(file, "#define offnextnative %3d\n", (int) OFFSET(native_stackframeinfo,next));
650 suck_init(classpath);
653 heap_init(heapsize, heapsize, &dummy);
657 /*********************** Load JAVA classes **************************/
659 nativemethod_chain = chain_new();
660 nativeclass_chain = chain_new();
662 for (a = 1; a < argc; a++) {
665 /* convert classname */
666 for (i = strlen(cp) - 1; i >= 0; i--) {
674 topclass = loader_load(utf_new_char(cp));
676 headerfile_generate(topclass);
681 /************************ Release all resources **********************/
685 tables_close(literalstring_free);
688 /* Print "finished" message */
690 log_text("Java - header-generator stopped");
698 void setVMClassField(classinfo *c)
703 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this) {
709 * These are local overrides for various environment variables in Emacs.
710 * Please do not remove this and leave it at the end of the file, where
711 * Emacs will automagically detect them.
712 * ---------------------------------------------------------------------
715 * indent-tabs-mode: t