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 862 2004-01-06 23:42:01Z stefan $
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 */
67 bool showmethods = false;
68 bool showconstantpool = false;
71 bool makeinitializations = true;
73 bool getloadingtime = false;
77 /******* replace some external functions *********/
79 functionptr native_findfunction(utf *cname, utf *mname, utf *desc, bool isstatic)
82 java_objectheader *javastring_new(utf *text)
86 void throw_noclassdeffounderror_message(utf* classname)
88 printf("Class not found: ");
89 utf_display(classname);
95 /* this is here to avoid link errors.
96 We are not interested in linkagerrors in cacaoh right now
98 void throw_linkageerror_message(utf* classname)
103 java_objectheader *literalstring_new(utf *u) { return NULL; }
106 void literalstring_free(java_objectheader *o) { }
107 void stringtable_update() { }
108 void synchronize_caches() { }
109 void asm_call_jit_compiler() { }
110 void asm_calljavafunction() { }
111 void asm_dumpregistersandcall() { }
112 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
114 s4 asm_builtin_idiv(s4 a, s4 b) {return 0;}
115 s4 asm_builtin_irem(s4 a, s4 b) {return 0;}
116 s8 asm_builtin_ldiv(s8 a, s8 b) {return 0;}
117 s8 asm_builtin_lrem(s8 a, s8 b) {return 0;}
119 s4 asm_builtin_f2i(float a) { return 0; }
120 s8 asm_builtin_f2l(float a) { return 0; }
121 s4 asm_builtin_d2i(double a) { return 0; }
122 s8 asm_builtin_d2l(double a) { return 0; }
124 void asm_builtin_monitorenter(java_objectheader *o) {}
125 void asm_builtin_monitorexit(java_objectheader *o) {}
127 s4 asm_builtin_checkarraycast(java_objectheader *obj, vftbl *target) {return 0;}
130 #if defined(__I386__)
131 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) { return 0; }
132 void asm_builtin_newarray(s4 size, vftbl *arrayvftbl) {}
135 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
137 u1 *createcompilerstub(methodinfo *m) {return NULL;}
138 u1 *createnativestub(functionptr f, methodinfo *m) {return NULL;}
139 u1 *oldcreatenativestub(functionptr f, methodinfo *m) {return NULL;}
141 void removecompilerstub(u1 *stub) {}
142 void removenativestub(u1 *stub) {}
144 void asm_perform_threadswitch(u1 **from, u1 **to) {}
145 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
146 void asm_switchstackandcall() {}
147 void asm_handle_builtin_exception(classinfo *c) {}
149 java_objectheader *native_new_and_init(void *p) { return NULL; }
152 /************************ global variables **********************/
154 THREADSPECIFIC java_objectheader *_exceptionptr;
156 static chain *nativemethod_chain; /* chain with native methods */
157 static chain *nativeclass_chain; /* chain with processed classes */
158 static chain *ident_chain; /* chain with method and field names in current class */
159 static FILE *file = NULL;
160 static u4 outputsize;
161 static bool dopadding;
164 static void printIDpart(int c)
166 if ((c >= 'a' && c <= 'z')
167 || (c >= 'A' && c <= 'Z')
168 || (c >= '0' && c <= '9')
176 static void printID(utf *u)
178 char *utf_ptr = u->text;
181 for (i = 0; i < utf_strlen(u); i++)
182 printIDpart(utf_nextu2(&utf_ptr));
186 static void addoutputsize (int len)
189 if (!dopadding) return;
191 newsize = ALIGN(outputsize, len);
193 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
194 outputsize = newsize;
198 static char *printtype(char *utf_ptr)
202 switch (utf_nextu2(&utf_ptr)) {
203 case 'V': fprintf (file, "void");
209 case 'Z': addoutputsize (4);
210 fprintf (file, "s4");
212 case 'J': addoutputsize (8);
213 fprintf (file, "s8");
215 case 'F': addoutputsize (4);
216 fprintf (file, "float");
218 case 'D': addoutputsize (8);
219 fprintf (file, "double");
222 addoutputsize ( sizeof(java_arrayheader*) );
223 switch (utf_nextu2(&utf_ptr)) {
224 case 'I': fprintf (file, "java_intarray*"); break;
225 case 'J': fprintf (file, "java_longarray*"); break;
226 case 'Z': fprintf (file, "java_booleanarray*"); break;
227 case 'B': fprintf (file, "java_bytearray*"); break;
228 case 'S': fprintf (file, "java_shortarray*"); break;
229 case 'C': fprintf (file, "java_chararray*"); break;
230 case 'F': fprintf (file, "java_floatarray*"); break;
231 case 'D': fprintf (file, "java_doublearray*"); break;
233 case '[': fprintf (file, "java_objectarray*");
234 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
236 while (utf_nextu2(&utf_ptr) != ';');
239 case 'L': fprintf (file, "java_objectarray*");
240 while ( utf_nextu2(&utf_ptr) != ';');
242 default: panic ("invalid type descriptor");
247 addoutputsize ( sizeof(java_objectheader*));
248 fprintf (file, "struct ");
249 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
253 default: panic ("Unknown type in field descriptor");
260 /***** determine the number of entries of a utf string in the ident chain *****/
262 static int searchidentchain_utf(utf *ident)
264 utf *u = chain_first(ident_chain); /* first element of list */
268 if (u==ident) count++; /* string found */
269 u = chain_next(ident_chain); /* next element in list */
276 /************** print structure for direct access to objects ******************/
278 static void printfields(classinfo *c)
285 addoutputsize(sizeof(java_objectheader));
286 fprintf(file, " java_objectheader header;\n");
290 printfields(c->super);
292 for (i = 0; i < c->fieldscount; i++) {
295 if (!(f->flags & ACC_STATIC)) {
297 printtype(f->descriptor->text);
299 utf_fprint(file, f->name);
301 /* rename multiple fieldnames */
302 if ((ident_count = searchidentchain_utf(f->name)))
303 fprintf(file, "%d", ident_count - 1);
304 chain_addlast(ident_chain, f->name);
306 fprintf(file, ";\n");
312 /***************** store prototype for native method in file ******************/
314 static void printmethod(methodinfo *m)
320 /* search for return-type in descriptor */
321 utf_ptr = m->descriptor->text;
322 while (utf_nextu2(&utf_ptr) != ')');
325 fprintf(file, "\n/*\n * Class: ");
326 utf_fprint(file, m->class->name);
327 fprintf(file, "\n * Method: ");
328 utf_fprint(file, m->name);
329 fprintf(file, "\n * Signature: ");
330 utf_fprint(file, m->descriptor);
331 fprintf(file, "\n */\n");
333 /* create prototype */
334 fprintf(file, "JNIEXPORT ");
336 fprintf(file, " JNICALL Java_");
337 printID(m->class->name);
339 /* rename overloaded method */
340 if ((ident_count = searchidentchain_utf(m->name)))
341 fprintf(file, "%d", ident_count - 1);
342 chain_addlast(ident_chain, m->name);
346 fprintf(file, "(JNIEnv *env");
348 utf_ptr = m->descriptor->text + 1;
350 if (!(m->flags & ACC_STATIC)) {
351 fprintf(file, ", struct ");
352 printID(m->class->name);
353 fprintf(file, "* this");
356 fprintf(file, ", jclass clazz");
359 if ((*utf_ptr) != ')') fprintf(file, ", ");
361 while ((*utf_ptr) != ')') {
362 utf_ptr = printtype(utf_ptr);
363 fprintf(file, " par%d", paramnum++);
364 if ((*utf_ptr)!=')') fprintf(file, ", ");
367 fprintf(file, ");\n\n");
371 /******* remove package-name in fully-qualified classname *********************/
373 static void gen_header_filename(char *buffer, utf *u)
377 for (i = 0;i < utf_strlen(u); i++) {
378 if ((u->text[i] == '/') || (u->text[i] == '$')) buffer[i] = '_'; /* convert '$' and '/' to '_' */
379 else buffer[i] = u->text[i];
381 buffer[utf_strlen(u)] = '\0';
385 /* create headerfile for classes and store native methods in chain ************/
387 static void headerfile_generate(classinfo *c)
389 char header_filename[1024] = "";
390 char classname[1024];
391 char uclassname[1024];
395 /* store class in chain */
396 chain_addlast(nativeclass_chain, c);
398 /* open headerfile for class */
399 gen_header_filename(classname, c->name);
401 /* create chain for renaming fields */
402 ident_chain = chain_new();
404 sprintf(header_filename, "nat/%s.h", classname);
405 file = fopen(header_filename, "w");
406 if (!file) panic("Can not open file to store header information");
408 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
410 /* convert to uppercase */
411 for (i = 0; classname[i]; i++) {
412 uclassname[i] = toupper(classname[i]);
414 uclassname[i] = '\0';
416 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
418 /* create structure for direct access to objects */
419 fprintf(file, "/* Structure information for class: ");
420 utf_fprint(file, c->name);
421 fprintf(file, " */\n\n");
422 fprintf(file, "typedef struct ");
424 fprintf(file, " {\n");
430 fprintf(file, ";\n\n");
432 /* create chain for renaming overloaded methods */
433 chain_free(ident_chain);
434 ident_chain = chain_new();
436 /* create method-prototypes */
438 for (i = 0; i < c->methodscount; i++) {
440 m = &(c->methods[i]);
442 if (m->flags & ACC_NATIVE) {
443 chain_addlast(nativemethod_chain, m);
448 chain_free(ident_chain);
450 fprintf(file, "#endif\n\n");
456 /******** print classname, '$' used to seperate inner-class name ***********/
458 void print_classname(classinfo *clazz)
460 utf *u = clazz->name;
461 char *endpos = u->text + u->blength;
462 char *utf_ptr = u->text;
465 while (utf_ptr < endpos) {
466 if ((c = utf_nextu2(&utf_ptr)) == '_') {
476 /*************** create table for locating native functions ****************/
478 static void printnativetableentry(methodinfo *m)
480 fprintf(file, " { \"");
481 print_classname(m->class);
482 fprintf(file, "\",\n \"");
483 utf_fprint(file, m->name);
484 fprintf(file, "\",\n \"");
485 utf_fprint(file, m->descriptor);
486 fprintf(file, "\",\n ");
488 if ((m->flags & ACC_STATIC) != 0)
489 fprintf(file, "true");
491 fprintf(file, "false");
493 fprintf(file, ",\n ");
494 fprintf(file, "(functionptr) Java_");
495 printID(m->class->name);
498 fprintf(file,"\n },\n");
502 /***************************************************************************
504 create the nativetypes-headerfile which includes
505 the headerfiles of the classes stored in the classes-chain
507 ****************************************************************************/
509 static void headers_finish()
513 char classname[1024];
515 file = fopen("nativetypes.hh", "w");
518 panic("Can not open file 'native.h' to store header information");
520 fprintf(file, "/* Headerfile for native methods: nativetypes.hh */\n");
521 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
522 fprintf(file, "\n/* include native-Headerfiles */\n\n");
524 c = chain_first(nativeclass_chain);
527 gen_header_filename(classname, c->name);
528 fprintf(file, "#include \"nat/%s.h\"\n", classname);
529 c = chain_next(nativeclass_chain);
533 chain_free(nativeclass_chain);
535 /* create table of native-methods */
537 file = fopen("nativetable.hh", "w");
540 panic("Can not open file 'nativetable' to store native-link-table");
542 fprintf(file, "/* Table of native methods: nativetables.hh */\n");
543 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
545 while ((m = chain_first(nativemethod_chain)) != NULL) {
546 chain_remove(nativemethod_chain);
547 printnativetableentry(m);
550 chain_free(nativemethod_chain);
555 /******************** internal function: print_usage ************************
557 Prints usage information for the JAVA header generator to stdout.
559 ***************************************************************************/
561 static void print_usage()
563 printf("Usage: cacaoh class [class..]\n");
568 /************************** Function: main *******************************
572 **************************************************************************/
574 int main(int argc, char **argv)
582 /********** internal (only used by main) *****************************/
584 char classpath[500] = "";
585 char offsets_filename[1024] = ""; /* phil */
586 u4 heapsize = 100000;
588 /*********** options so only headers are generated *******************/
590 makeinitializations = false;
593 /************ Collect some info from the environment *****************/
595 cp = getenv("CLASSPATH");
597 strcpy(classpath + strlen(classpath), ":");
598 strcpy(classpath + strlen(classpath), cp);
607 /**************************** Program start **************************/
610 log_text("Java - header-generator started");
612 sprintf(offsets_filename, "jit/%s/offsets.h", ARCH_DIR);
613 file = fopen(offsets_filename, "w");
615 fprintf(stderr, "Can not open file '%s' for write", offsets_filename);
619 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
621 fprintf(file, "#define offobjvftbl %3d\n", (int) OFFSET(java_objectheader, vftbl));
622 fprintf(file, "#define offarraysize %3d\n", (int) OFFSET(java_arrayheader, size));
623 fprintf(file, "#define offobjarrdata %3d\n\n", (int) OFFSET(java_objectarray, data[0]));
624 fprintf(file, "#define offbaseval %3d\n", (int) OFFSET(vftbl, baseval));
625 fprintf(file, "#define offdiffval %3d\n\n", (int) OFFSET(vftbl, diffval));
627 fprintf(file, "#define offclassvftbl %3d\n", (int) OFFSET(classinfo, vftbl));
628 fprintf(file, "#define offclassinit %3d\n\n", (int) OFFSET(classinfo, initialized));
630 fprintf(file, "#define offjniitemtype %3d\n", (int) OFFSET(jni_callblock, itemtype));
631 fprintf(file, "#define offjniitem %3d\n", (int) OFFSET(jni_callblock, item));
632 fprintf(file, "#define sizejniblock %3d\n\n", (int) sizeof(jni_callblock));
637 suck_init(classpath);
640 heap_init(heapsize, heapsize, &dummy);
644 /*********************** Load JAVA classes **************************/
646 nativemethod_chain = chain_new();
647 nativeclass_chain = chain_new();
649 for (a = 1; a < argc; a++) {
652 /* convert classname */
653 for (i = strlen(cp) - 1; i >= 0; i--) {
661 topclass = loader_load(utf_new_char(cp));
663 headerfile_generate(topclass);
668 /************************ Release all resources **********************/
672 tables_close(literalstring_free);
675 /* Print "finished" message */
677 log_text("Java - header-generator stopped");
685 void setVMClassField(classinfo *c)
690 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this) {
696 * These are local overrides for various environment variables in Emacs.
697 * Please do not remove this and leave it at the end of the file, where
698 * Emacs will automagically detect them.
699 * ---------------------------------------------------------------------
702 * indent-tabs-mode: t