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 895 2004-01-19 13:53:43Z edwin $
46 #include "toolbox/chain.h"
47 #include "toolbox/memory.h"
48 #include "toolbox/loging.h"
51 /* replace command line options */
54 bool compileall = false;
55 bool runverbose = false;
56 bool collectverbose = false;
58 bool loadverbose = false;
59 bool linkverbose = false;
60 bool initverbose = false;
62 bool opt_rt = false; /* true if RTA parse should be used RT-CO */
63 bool opt_xta = false; /* true if XTA parse should be used XTA-CO */
64 bool opt_vta = false; /* true if VTA parse should be used VTA-CO */
65 bool opt_verify = true; /* true if classfiles should be verified */
66 bool opt_liberalutf = false; /* Don't check overlong UTF-8 sequences */
68 bool showmethods = false;
69 bool showconstantpool = false;
72 bool makeinitializations = true;
74 bool getloadingtime = false;
78 /******* replace some external functions *********/
80 functionptr native_findfunction(utf *cname, utf *mname, utf *desc, bool isstatic)
83 java_objectheader *javastring_new(utf *text)
87 void throw_noclassdeffounderror_message(utf* classname)
89 printf("Class not found: ");
90 utf_display(classname);
96 /* this is here to avoid link errors.
97 We are not interested in linkagerrors in cacaoh right now
99 void throw_linkageerror_message(utf* classname)
104 java_objectheader *literalstring_new(utf *u) { return NULL; }
107 void literalstring_free(java_objectheader *o) { }
108 void stringtable_update() { }
109 void synchronize_caches() { }
110 void asm_call_jit_compiler() { }
111 void asm_calljavafunction() { }
112 void asm_dumpregistersandcall() { }
113 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
115 s4 asm_builtin_idiv(s4 a, s4 b) {return 0;}
116 s4 asm_builtin_irem(s4 a, s4 b) {return 0;}
117 s8 asm_builtin_ldiv(s8 a, s8 b) {return 0;}
118 s8 asm_builtin_lrem(s8 a, s8 b) {return 0;}
120 s4 asm_builtin_f2i(float a) { return 0; }
121 s8 asm_builtin_f2l(float a) { return 0; }
122 s4 asm_builtin_d2i(double a) { return 0; }
123 s8 asm_builtin_d2l(double a) { return 0; }
125 void asm_builtin_monitorenter(java_objectheader *o) {}
126 void asm_builtin_monitorexit(java_objectheader *o) {}
128 s4 asm_builtin_checkarraycast(java_objectheader *obj, vftbl *target) {return 0;}
131 #if defined(__I386__)
132 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) { return 0; }
133 void asm_builtin_newarray(s4 size, vftbl *arrayvftbl) {}
136 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
138 u1 *createcompilerstub(methodinfo *m) {return NULL;}
139 u1 *createnativestub(functionptr f, methodinfo *m) {return NULL;}
140 u1 *oldcreatenativestub(functionptr f, methodinfo *m) {return NULL;}
142 void removecompilerstub(u1 *stub) {}
143 void removenativestub(u1 *stub) {}
145 void asm_perform_threadswitch(u1 **from, u1 **to) {}
146 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
147 void asm_switchstackandcall() {}
148 void asm_handle_builtin_exception(classinfo *c) {}
150 java_objectheader *native_new_and_init(void *p) { return NULL; }
153 /************************ global variables **********************/
155 THREADSPECIFIC java_objectheader *_exceptionptr;
157 static chain *nativemethod_chain; /* chain with native methods */
158 static chain *nativeclass_chain; /* chain with processed classes */
159 static chain *ident_chain; /* chain with method and field names in current class */
160 static FILE *file = NULL;
161 static u4 outputsize;
162 static bool dopadding;
165 static void printIDpart(int c)
167 if ((c >= 'a' && c <= 'z')
168 || (c >= 'A' && c <= 'Z')
169 || (c >= '0' && c <= '9')
177 static void printID(utf *u)
179 char *utf_ptr = u->text;
182 for (i = 0; i < utf_strlen(u); i++)
183 printIDpart(utf_nextu2(&utf_ptr));
187 static void addoutputsize (int len)
190 if (!dopadding) return;
192 newsize = ALIGN(outputsize, len);
194 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
195 outputsize = newsize;
199 static char *printtype(char *utf_ptr)
203 switch (utf_nextu2(&utf_ptr)) {
204 case 'V': fprintf (file, "void");
210 case 'Z': addoutputsize (4);
211 fprintf (file, "s4");
213 case 'J': addoutputsize (8);
214 fprintf (file, "s8");
216 case 'F': addoutputsize (4);
217 fprintf (file, "float");
219 case 'D': addoutputsize (8);
220 fprintf (file, "double");
223 addoutputsize ( sizeof(java_arrayheader*) );
224 switch (utf_nextu2(&utf_ptr)) {
225 case 'I': fprintf (file, "java_intarray*"); break;
226 case 'J': fprintf (file, "java_longarray*"); break;
227 case 'Z': fprintf (file, "java_booleanarray*"); break;
228 case 'B': fprintf (file, "java_bytearray*"); break;
229 case 'S': fprintf (file, "java_shortarray*"); break;
230 case 'C': fprintf (file, "java_chararray*"); break;
231 case 'F': fprintf (file, "java_floatarray*"); break;
232 case 'D': fprintf (file, "java_doublearray*"); break;
234 case '[': fprintf (file, "java_objectarray*");
235 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
237 while (utf_nextu2(&utf_ptr) != ';');
240 case 'L': fprintf (file, "java_objectarray*");
241 while ( utf_nextu2(&utf_ptr) != ';');
243 default: panic ("invalid type descriptor");
248 addoutputsize ( sizeof(java_objectheader*));
249 fprintf (file, "struct ");
250 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
254 default: panic ("Unknown type in field descriptor");
261 /***** determine the number of entries of a utf string in the ident chain *****/
263 static int searchidentchain_utf(utf *ident)
265 utf *u = chain_first(ident_chain); /* first element of list */
269 if (u==ident) count++; /* string found */
270 u = chain_next(ident_chain); /* next element in list */
277 /************** print structure for direct access to objects ******************/
279 static void printfields(classinfo *c)
286 addoutputsize(sizeof(java_objectheader));
287 fprintf(file, " java_objectheader header;\n");
291 printfields(c->super);
293 for (i = 0; i < c->fieldscount; i++) {
296 if (!(f->flags & ACC_STATIC)) {
298 printtype(f->descriptor->text);
300 utf_fprint(file, f->name);
302 /* rename multiple fieldnames */
303 if ((ident_count = searchidentchain_utf(f->name)))
304 fprintf(file, "%d", ident_count - 1);
305 chain_addlast(ident_chain, f->name);
307 fprintf(file, ";\n");
313 /***************** store prototype for native method in file ******************/
315 static void printmethod(methodinfo *m)
321 /* search for return-type in descriptor */
322 utf_ptr = m->descriptor->text;
323 while (utf_nextu2(&utf_ptr) != ')');
326 fprintf(file, "\n/*\n * Class: ");
327 utf_fprint(file, m->class->name);
328 fprintf(file, "\n * Method: ");
329 utf_fprint(file, m->name);
330 fprintf(file, "\n * Signature: ");
331 utf_fprint(file, m->descriptor);
332 fprintf(file, "\n */\n");
334 /* create prototype */
335 fprintf(file, "JNIEXPORT ");
337 fprintf(file, " JNICALL Java_");
338 printID(m->class->name);
340 /* rename overloaded method */
341 if ((ident_count = searchidentchain_utf(m->name)))
342 fprintf(file, "%d", ident_count - 1);
343 chain_addlast(ident_chain, m->name);
347 fprintf(file, "(JNIEnv *env");
349 utf_ptr = m->descriptor->text + 1;
351 if (!(m->flags & ACC_STATIC)) {
352 fprintf(file, ", struct ");
353 printID(m->class->name);
354 fprintf(file, "* this");
357 fprintf(file, ", jclass clazz");
360 if ((*utf_ptr) != ')') fprintf(file, ", ");
362 while ((*utf_ptr) != ')') {
363 utf_ptr = printtype(utf_ptr);
364 fprintf(file, " par%d", paramnum++);
365 if ((*utf_ptr)!=')') fprintf(file, ", ");
368 fprintf(file, ");\n\n");
372 /******* remove package-name in fully-qualified classname *********************/
374 static void gen_header_filename(char *buffer, utf *u)
378 for (i = 0;i < utf_strlen(u); i++) {
379 if ((u->text[i] == '/') || (u->text[i] == '$')) buffer[i] = '_'; /* convert '$' and '/' to '_' */
380 else buffer[i] = u->text[i];
382 buffer[utf_strlen(u)] = '\0';
386 /* create headerfile for classes and store native methods in chain ************/
388 static void headerfile_generate(classinfo *c)
390 char header_filename[1024] = "";
391 char classname[1024];
392 char uclassname[1024];
396 /* store class in chain */
397 chain_addlast(nativeclass_chain, c);
399 /* open headerfile for class */
400 gen_header_filename(classname, c->name);
402 /* create chain for renaming fields */
403 ident_chain = chain_new();
405 sprintf(header_filename, "nat/%s.h", classname);
406 file = fopen(header_filename, "w");
407 if (!file) panic("Can not open file to store header information");
409 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
411 /* convert to uppercase */
412 for (i = 0; classname[i]; i++) {
413 uclassname[i] = toupper(classname[i]);
415 uclassname[i] = '\0';
417 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
419 /* create structure for direct access to objects */
420 fprintf(file, "/* Structure information for class: ");
421 utf_fprint(file, c->name);
422 fprintf(file, " */\n\n");
423 fprintf(file, "typedef struct ");
425 fprintf(file, " {\n");
431 fprintf(file, ";\n\n");
433 /* create chain for renaming overloaded methods */
434 chain_free(ident_chain);
435 ident_chain = chain_new();
437 /* create method-prototypes */
439 for (i = 0; i < c->methodscount; i++) {
441 m = &(c->methods[i]);
443 if (m->flags & ACC_NATIVE) {
444 chain_addlast(nativemethod_chain, m);
449 chain_free(ident_chain);
451 fprintf(file, "#endif\n\n");
457 /******** print classname, '$' used to seperate inner-class name ***********/
459 void print_classname(classinfo *clazz)
461 utf *u = clazz->name;
462 char *endpos = u->text + u->blength;
463 char *utf_ptr = u->text;
466 while (utf_ptr < endpos) {
467 if ((c = utf_nextu2(&utf_ptr)) == '_') {
477 /*************** create table for locating native functions ****************/
479 static void printnativetableentry(methodinfo *m)
481 fprintf(file, " { \"");
482 print_classname(m->class);
483 fprintf(file, "\",\n \"");
484 utf_fprint(file, m->name);
485 fprintf(file, "\",\n \"");
486 utf_fprint(file, m->descriptor);
487 fprintf(file, "\",\n ");
489 if ((m->flags & ACC_STATIC) != 0)
490 fprintf(file, "true");
492 fprintf(file, "false");
494 fprintf(file, ",\n ");
495 fprintf(file, "(functionptr) Java_");
496 printID(m->class->name);
499 fprintf(file,"\n },\n");
503 /***************************************************************************
505 create the nativetypes-headerfile which includes
506 the headerfiles of the classes stored in the classes-chain
508 ****************************************************************************/
510 static void headers_finish()
514 char classname[1024];
516 file = fopen("nativetypes.hh", "w");
519 panic("Can not open file 'native.h' to store header information");
521 fprintf(file, "/* Headerfile for native methods: nativetypes.hh */\n");
522 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
523 fprintf(file, "\n/* include native-Headerfiles */\n\n");
525 c = chain_first(nativeclass_chain);
528 gen_header_filename(classname, c->name);
529 fprintf(file, "#include \"nat/%s.h\"\n", classname);
530 c = chain_next(nativeclass_chain);
534 chain_free(nativeclass_chain);
536 /* create table of native-methods */
538 file = fopen("nativetable.hh", "w");
541 panic("Can not open file 'nativetable' to store native-link-table");
543 fprintf(file, "/* Table of native methods: nativetables.hh */\n");
544 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
546 while ((m = chain_first(nativemethod_chain)) != NULL) {
547 chain_remove(nativemethod_chain);
548 printnativetableentry(m);
551 chain_free(nativemethod_chain);
556 /******************** internal function: print_usage ************************
558 Prints usage information for the JAVA header generator to stdout.
560 ***************************************************************************/
562 static void print_usage()
564 printf("Usage: cacaoh class [class..]\n");
569 /************************** Function: main *******************************
573 **************************************************************************/
575 int main(int argc, char **argv)
583 /********** internal (only used by main) *****************************/
585 char classpath[500] = "";
586 char offsets_filename[1024] = ""; /* phil */
587 u4 heapsize = 100000;
589 /*********** options so only headers are generated *******************/
591 makeinitializations = false;
594 /************ Collect some info from the environment *****************/
596 cp = getenv("CLASSPATH");
598 strcpy(classpath + strlen(classpath), ":");
599 strcpy(classpath + strlen(classpath), cp);
608 /**************************** Program start **************************/
611 log_text("Java - header-generator started");
613 sprintf(offsets_filename, "jit/%s/offsets.h", ARCH_DIR);
614 file = fopen(offsets_filename, "w");
616 fprintf(stderr, "Can not open file '%s' for write", offsets_filename);
620 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
622 fprintf(file, "#define offobjvftbl %3d\n", (int) OFFSET(java_objectheader, vftbl));
623 fprintf(file, "#define offarraysize %3d\n", (int) OFFSET(java_arrayheader, size));
624 fprintf(file, "#define offobjarrdata %3d\n\n", (int) OFFSET(java_objectarray, data[0]));
625 fprintf(file, "#define offbaseval %3d\n", (int) OFFSET(vftbl, baseval));
626 fprintf(file, "#define offdiffval %3d\n\n", (int) OFFSET(vftbl, diffval));
628 fprintf(file, "#define offclassvftbl %3d\n", (int) OFFSET(classinfo, vftbl));
629 fprintf(file, "#define offclassinit %3d\n\n", (int) OFFSET(classinfo, initialized));
631 fprintf(file, "#define offjniitemtype %3d\n", (int) OFFSET(jni_callblock, itemtype));
632 fprintf(file, "#define offjniitem %3d\n", (int) OFFSET(jni_callblock, item));
633 fprintf(file, "#define sizejniblock %3d\n\n", (int) sizeof(jni_callblock));
638 suck_init(classpath);
641 heap_init(heapsize, heapsize, &dummy);
645 /*********************** Load JAVA classes **************************/
647 nativemethod_chain = chain_new();
648 nativeclass_chain = chain_new();
650 for (a = 1; a < argc; a++) {
653 /* convert classname */
654 for (i = strlen(cp) - 1; i >= 0; i--) {
662 topclass = loader_load(utf_new_char(cp));
664 headerfile_generate(topclass);
669 /************************ Release all resources **********************/
673 tables_close(literalstring_free);
676 /* Print "finished" message */
678 log_text("Java - header-generator stopped");
686 void setVMClassField(classinfo *c)
691 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this) {
697 * These are local overrides for various environment variables in Emacs.
698 * Please do not remove this and leave it at the end of the file, where
699 * Emacs will automagically detect them.
700 * ---------------------------------------------------------------------
703 * indent-tabs-mode: t