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 800 2003-12-16 22:47:59Z edwin $
44 #include "toolbox/chain.h"
45 #include "toolbox/memory.h"
46 #include "toolbox/loging.h"
49 /* replace command line options */
52 bool compileall = false;
53 bool runverbose = false;
54 bool collectverbose = false;
56 bool loadverbose = false;
57 bool linkverbose = false;
58 bool initverbose = false;
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 bool opt_verify = true; /* true if classfiles should be verified */
65 bool showmethods = false;
66 bool showconstantpool = false;
69 bool makeinitializations = true;
71 bool getloadingtime = false;
75 /******* replace some external functions *********/
77 functionptr native_findfunction(utf *cname, utf *mname, utf *desc, bool isstatic)
80 java_objectheader *javastring_new(utf *text)
84 void throw_noclassdeffounderror_message(utf* classname)
86 printf("Class not found: ");
87 utf_display(classname);
93 /* this is here to avoid link errors.
94 We are not interested in linkagerrors in cacaoh right now
96 void throw_linkageerror_message(utf* classname)
101 java_objectheader *literalstring_new(utf *u) { return NULL; }
104 void literalstring_free(java_objectheader *o) { }
105 void stringtable_update() { }
106 void synchronize_caches() { }
107 void asm_call_jit_compiler() { }
108 void asm_calljavafunction() { }
109 void asm_dumpregistersandcall() { }
110 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
112 s4 asm_builtin_idiv(s4 a, s4 b) {return 0;}
113 s4 asm_builtin_irem(s4 a, s4 b) {return 0;}
114 s8 asm_builtin_ldiv(s8 a, s8 b) {return 0;}
115 s8 asm_builtin_lrem(s8 a, s8 b) {return 0;}
117 s4 asm_builtin_f2i(float a) { return 0; }
118 s8 asm_builtin_f2l(float a) { return 0; }
119 s4 asm_builtin_d2i(double a) { return 0; }
120 s8 asm_builtin_d2l(double a) { return 0; }
122 void asm_builtin_monitorenter(java_objectheader *o) {}
123 void asm_builtin_monitorexit(java_objectheader *o) {}
125 s4 asm_builtin_checkarraycast(java_objectheader *o,arraydescriptor *d) {return 0;}
127 #if defined(__I386__)
128 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) { return 0; }
129 void asm_builtin_newarray(s4 size, vftbl *arrayvftbl) {}
132 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
134 u1 *createcompilerstub(methodinfo *m) {return NULL;}
135 u1 *createnativestub(functionptr f, methodinfo *m) {return NULL;}
136 u1 *oldcreatenativestub(functionptr f, methodinfo *m) {return NULL;}
138 void removecompilerstub(u1 *stub) {}
139 void removenativestub(u1 *stub) {}
141 void asm_perform_threadswitch(u1 **from, u1 **to) {}
142 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
143 void asm_switchstackandcall() {}
144 void asm_handle_builtin_exception(classinfo *c) {}
146 java_objectheader *native_new_and_init(void *p) { return NULL; }
149 /************************ global variables **********************/
151 java_objectheader *exceptionptr;
153 static chain *nativemethod_chain; /* chain with native methods */
154 static chain *nativeclass_chain; /* chain with processed classes */
155 static chain *ident_chain; /* chain with method and field names in current class */
156 static FILE *file = NULL;
157 static u4 outputsize;
158 static bool dopadding;
161 static void printIDpart(int c)
163 if ((c >= 'a' && c <= 'z')
164 || (c >= 'A' && c <= 'Z')
165 || (c >= '0' && c <= '9')
173 static void printID(utf *u)
175 char *utf_ptr = u->text;
178 for (i = 0; i < utf_strlen(u); i++)
179 printIDpart(utf_nextu2(&utf_ptr));
183 static void addoutputsize (int len)
186 if (!dopadding) return;
188 newsize = ALIGN(outputsize, len);
190 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
191 outputsize = newsize;
195 static char *printtype(char *utf_ptr)
199 switch (utf_nextu2(&utf_ptr)) {
200 case 'V': fprintf (file, "void");
206 case 'Z': addoutputsize (4);
207 fprintf (file, "s4");
209 case 'J': addoutputsize (8);
210 fprintf (file, "s8");
212 case 'F': addoutputsize (4);
213 fprintf (file, "float");
215 case 'D': addoutputsize (8);
216 fprintf (file, "double");
219 addoutputsize ( sizeof(java_arrayheader*) );
220 switch (utf_nextu2(&utf_ptr)) {
221 case 'I': fprintf (file, "java_intarray*"); break;
222 case 'J': fprintf (file, "java_longarray*"); break;
223 case 'Z': fprintf (file, "java_booleanarray*"); break;
224 case 'B': fprintf (file, "java_bytearray*"); break;
225 case 'S': fprintf (file, "java_shortarray*"); break;
226 case 'C': fprintf (file, "java_chararray*"); break;
227 case 'F': fprintf (file, "java_floatarray*"); break;
228 case 'D': fprintf (file, "java_doublearray*"); break;
230 case '[': fprintf (file, "java_objectarray*");
231 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
233 while (utf_nextu2(&utf_ptr) != ';');
236 case 'L': fprintf (file, "java_objectarray*");
237 while ( utf_nextu2(&utf_ptr) != ';');
239 default: panic ("invalid type descriptor");
244 addoutputsize ( sizeof(java_objectheader*));
245 fprintf (file, "struct ");
246 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
250 default: panic ("Unknown type in field descriptor");
257 /******* determine the number of entries of a utf string in the ident chain *****/
259 static int searchidentchain_utf(utf *ident)
261 utf *u = chain_first(ident_chain); /* first element of list */
265 if (u==ident) count++; /* string found */
266 u = chain_next(ident_chain); /* next element in list */
273 /**************** print structure for direct access to objects ******************/
275 static void printfields(classinfo *c)
282 addoutputsize(sizeof(java_objectheader));
283 fprintf(file, " java_objectheader header;\n");
287 printfields(c->super);
289 for (i = 0; i < c->fieldscount; i++) {
292 if (!(f->flags & ACC_STATIC)) {
294 printtype(f->descriptor->text);
296 utf_fprint(file, f->name);
298 /* rename multiple fieldnames */
299 if ((ident_count = searchidentchain_utf(f->name)))
300 fprintf(file, "%d", ident_count - 1);
301 chain_addlast(ident_chain, f->name);
303 fprintf(file, ";\n");
309 /***************** store prototype for native method in file ******************/
311 static void printmethod(methodinfo *m)
317 /* search for return-type in descriptor */
318 utf_ptr = m->descriptor->text;
319 while (utf_nextu2(&utf_ptr) != ')');
322 fprintf(file, "\n/*\n * Class: ");
323 utf_fprint(file, m->class->name);
324 fprintf(file, "\n * Method: ");
325 utf_fprint(file, m->name);
326 fprintf(file, "\n * Signature: ");
327 utf_fprint(file, m->descriptor);
328 fprintf(file, "\n */\n");
330 /* create prototype */
331 fprintf(file, "JNIEXPORT ");
333 fprintf(file, " JNICALL Java_");
334 printID(m->class->name);
336 /* rename overloaded method */
337 if ((ident_count = searchidentchain_utf(m->name)))
338 fprintf(file, "%d", ident_count - 1);
339 chain_addlast(ident_chain, m->name);
343 fprintf(file, "(JNIEnv *env");
345 utf_ptr = m->descriptor->text + 1;
347 if (!(m->flags & ACC_STATIC)) {
348 fprintf(file, ", struct ");
349 printID(m->class->name);
350 fprintf(file, "* this");
353 fprintf(file, ", jclass clazz");
356 if ((*utf_ptr) != ')') fprintf(file, ", ");
358 while ((*utf_ptr) != ')') {
359 utf_ptr = printtype(utf_ptr);
360 fprintf(file, " par%d", paramnum++);
361 if ((*utf_ptr)!=')') fprintf(file, ", ");
364 fprintf(file, ");\n\n");
368 /****************** remove package-name in fully-qualified classname *********************/
370 static void gen_header_filename(char *buffer, utf *u)
374 for (i = 0;i < utf_strlen(u); i++) {
375 if ((u->text[i] == '/') || (u->text[i] == '$')) buffer[i] = '_'; /* convert '$' and '/' to '_' */
376 else buffer[i] = u->text[i];
378 buffer[utf_strlen(u)] = '\0';
382 /*********** create headerfile for classes and store native methods in chain ************/
384 static void headerfile_generate(classinfo *c)
386 char header_filename[1024] = "";
387 char classname[1024];
388 char uclassname[1024];
392 /* store class in chain */
393 chain_addlast(nativeclass_chain, c);
395 /* open headerfile for class */
396 gen_header_filename(classname, c->name);
398 /* create chain for renaming fields */
399 ident_chain = chain_new();
401 sprintf(header_filename, "nat/%s.h", classname);
402 file = fopen(header_filename, "w");
403 if (!file) panic("Can not open file to store header information");
405 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
407 /* convert to uppercase */
408 for (i = 0; classname[i]; i++) {
409 uclassname[i] = toupper(classname[i]);
411 uclassname[i] = '\0';
413 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
415 /* create structure for direct access to objects */
416 fprintf(file, "/* Structure information for class: ");
417 utf_fprint(file, c->name);
418 fprintf(file, " */\n\n");
419 fprintf(file, "typedef struct ");
421 fprintf(file, " {\n");
427 fprintf(file, ";\n\n");
429 /* create chain for renaming overloaded methods */
430 chain_free(ident_chain);
431 ident_chain = chain_new();
433 /* create method-prototypes */
435 for (i = 0; i < c->methodscount; i++) {
437 m = &(c->methods[i]);
439 if (m->flags & ACC_NATIVE) {
440 chain_addlast(nativemethod_chain, m);
445 chain_free(ident_chain);
447 fprintf(file, "#endif\n\n");
453 /******** print classname, '$' used to seperate inner-class name ***********/
455 void print_classname(classinfo *clazz)
457 utf *u = clazz->name;
458 char *endpos = u->text + u->blength;
459 char *utf_ptr = u->text;
462 while (utf_ptr < endpos) {
463 if ((c = utf_nextu2(&utf_ptr)) == '_') {
473 /*************** create table for locating native functions ****************/
475 static void printnativetableentry(methodinfo *m)
477 fprintf(file, " { \"");
478 print_classname(m->class);
479 fprintf(file, "\",\n \"");
480 utf_fprint(file, m->name);
481 fprintf(file, "\",\n \"");
482 utf_fprint(file, m->descriptor);
483 fprintf(file, "\",\n ");
485 if ((m->flags & ACC_STATIC) != 0)
486 fprintf(file, "true");
488 fprintf(file, "false");
490 fprintf(file, ",\n ");
491 fprintf(file, "(functionptr) Java_");
492 printID(m->class->name);
495 fprintf(file,"\n },\n");
499 /***************************************************************************
501 create the nativetypes-headerfile which includes
502 the headerfiles of the classes stored in the classes-chain
504 ****************************************************************************/
506 static void headers_finish()
510 char classname[1024];
512 file = fopen("nativetypes.hh", "w");
515 panic("Can not open file 'native.h' to store header information");
517 fprintf(file, "/* Headerfile for native methods: nativetypes.hh */\n");
518 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
519 fprintf(file, "\n/* include native-Headerfiles */\n\n");
521 c = chain_first(nativeclass_chain);
524 gen_header_filename(classname, c->name);
525 fprintf(file, "#include \"nat/%s.h\"\n", classname);
526 c = chain_next(nativeclass_chain);
530 chain_free(nativeclass_chain);
532 /* create table of native-methods */
534 file = fopen("nativetable.hh", "w");
537 panic("Can not open file 'nativetable' to store native-link-table");
539 fprintf(file, "/* Table of native methods: nativetables.hh */\n");
540 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
542 while ((m = chain_first(nativemethod_chain)) != NULL) {
543 chain_remove(nativemethod_chain);
544 printnativetableentry(m);
547 chain_free(nativemethod_chain);
552 /******************** internal function: print_usage ************************
554 Prints usage information for the JAVA header generator to stdout.
556 ***************************************************************************/
558 static void print_usage()
560 printf("Usage: cacaoh class [class..]\n");
565 /************************** Function: main *******************************
569 **************************************************************************/
571 int main(int argc, char **argv)
579 /********** internal (only used by main) *****************************/
581 char classpath[500] = "";
582 char offsets_filename[1024] = ""; /* phil */
583 u4 heapsize = 100000;
585 /*********** options so only headers are generated *******************/
587 makeinitializations = false;
590 /************ Collect some info from the environment *****************/
592 cp = getenv("CLASSPATH");
594 strcpy(classpath + strlen(classpath), ":");
595 strcpy(classpath + strlen(classpath), cp);
604 /**************************** Program start **************************/
607 log_text("Java - header-generator started");
609 sprintf(offsets_filename, "jit/%s/offsets.h", ARCH_DIR);
610 file = fopen(offsets_filename, "w");
612 fprintf(stderr, "Can not open file '%s' for write", offsets_filename);
616 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
618 fprintf(file, "#define offobjvftbl %3d\n", (int) OFFSET(java_objectheader, vftbl));
619 fprintf(file, "#define offarraysize %3d\n", (int) OFFSET(java_arrayheader, size));
620 fprintf(file, "#define offobjarrdata %3d\n\n", (int) OFFSET(java_objectarray, data[0]));
621 fprintf(file, "#define offbaseval %3d\n", (int) OFFSET(vftbl, baseval));
622 fprintf(file, "#define offdiffval %3d\n\n", (int) OFFSET(vftbl, diffval));
624 fprintf(file, "#define offclassvftbl %3d\n", (int) OFFSET(classinfo, vftbl));
626 fprintf(file, "#define offjniitemtype %3d\n", (int) OFFSET(jni_callblock, itemtype));
627 fprintf(file, "#define offjniitem %3d\n", (int) OFFSET(jni_callblock, item));
628 fprintf(file, "#define sizejniblock %3d\n\n", (int) sizeof(jni_callblock));
633 suck_init(classpath);
636 heap_init(heapsize, heapsize, &dummy);
640 /*********************** Load JAVA classes **************************/
642 nativemethod_chain = chain_new();
643 nativeclass_chain = chain_new();
645 for (a = 1; a < argc; a++) {
648 /* convert classname */
649 for (i = strlen(cp) - 1; i >= 0; i--) {
657 topclass = loader_load(utf_new_char(cp));
659 headerfile_generate(topclass);
664 /************************ Release all resources **********************/
668 tables_close(literalstring_free);
671 /* Print "finished" message */
673 log_text("Java - header-generator stopped");
681 void setVMClassField(classinfo *c)
686 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this) {
692 * These are local overrides for various environment variables in Emacs.
693 * Please do not remove this and leave it at the end of the file, where
694 * Emacs will automagically detect them.
695 * ---------------------------------------------------------------------
698 * indent-tabs-mode: t