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 809 2003-12-30 20:54:57Z twisti $
45 #include "toolbox/chain.h"
46 #include "toolbox/memory.h"
47 #include "toolbox/loging.h"
50 /* replace command line options */
53 bool compileall = false;
54 bool runverbose = false;
55 bool collectverbose = false;
57 bool loadverbose = false;
58 bool linkverbose = false;
59 bool initverbose = false;
61 bool opt_rt = false; /* true if RTA parse should be used RT-CO */
62 bool opt_xta = false; /* true if XTA parse should be used XTA-CO */
63 bool opt_vta = false; /* true if VTA parse should be used VTA-CO */
64 bool opt_verify = true; /* true if classfiles should be verified */
66 bool showmethods = false;
67 bool showconstantpool = false;
70 bool makeinitializations = true;
72 bool getloadingtime = false;
76 /******* replace some external functions *********/
78 functionptr native_findfunction(utf *cname, utf *mname, utf *desc, bool isstatic)
81 java_objectheader *javastring_new(utf *text)
85 void throw_noclassdeffounderror_message(utf* classname)
87 printf("Class not found: ");
88 utf_display(classname);
94 /* this is here to avoid link errors.
95 We are not interested in linkagerrors in cacaoh right now
97 void throw_linkageerror_message(utf* classname)
102 java_objectheader *literalstring_new(utf *u) { return NULL; }
105 void literalstring_free(java_objectheader *o) { }
106 void stringtable_update() { }
107 void synchronize_caches() { }
108 void asm_call_jit_compiler() { }
109 void asm_calljavafunction() { }
110 void asm_dumpregistersandcall() { }
111 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
113 s4 asm_builtin_idiv(s4 a, s4 b) {return 0;}
114 s4 asm_builtin_irem(s4 a, s4 b) {return 0;}
115 s8 asm_builtin_ldiv(s8 a, s8 b) {return 0;}
116 s8 asm_builtin_lrem(s8 a, s8 b) {return 0;}
118 s4 asm_builtin_f2i(float a) { return 0; }
119 s8 asm_builtin_f2l(float a) { return 0; }
120 s4 asm_builtin_d2i(double a) { return 0; }
121 s8 asm_builtin_d2l(double a) { return 0; }
123 void asm_builtin_monitorenter(java_objectheader *o) {}
124 void asm_builtin_monitorexit(java_objectheader *o) {}
126 s4 asm_builtin_checkarraycast(java_objectheader *o,arraydescriptor *d) {return 0;}
128 #if defined(__I386__)
129 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) { return 0; }
130 void asm_builtin_newarray(s4 size, vftbl *arrayvftbl) {}
133 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
135 u1 *createcompilerstub(methodinfo *m) {return NULL;}
136 u1 *createnativestub(functionptr f, methodinfo *m) {return NULL;}
137 u1 *oldcreatenativestub(functionptr f, methodinfo *m) {return NULL;}
139 void removecompilerstub(u1 *stub) {}
140 void removenativestub(u1 *stub) {}
142 void asm_perform_threadswitch(u1 **from, u1 **to) {}
143 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
144 void asm_switchstackandcall() {}
145 void asm_handle_builtin_exception(classinfo *c) {}
147 java_objectheader *native_new_and_init(void *p) { return NULL; }
150 /************************ global variables **********************/
152 java_objectheader *exceptionptr;
154 static chain *nativemethod_chain; /* chain with native methods */
155 static chain *nativeclass_chain; /* chain with processed classes */
156 static chain *ident_chain; /* chain with method and field names in current class */
157 static FILE *file = NULL;
158 static u4 outputsize;
159 static bool dopadding;
162 static void printIDpart(int c)
164 if ((c >= 'a' && c <= 'z')
165 || (c >= 'A' && c <= 'Z')
166 || (c >= '0' && c <= '9')
174 static void printID(utf *u)
176 char *utf_ptr = u->text;
179 for (i = 0; i < utf_strlen(u); i++)
180 printIDpart(utf_nextu2(&utf_ptr));
184 static void addoutputsize (int len)
187 if (!dopadding) return;
189 newsize = ALIGN(outputsize, len);
191 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
192 outputsize = newsize;
196 static char *printtype(char *utf_ptr)
200 switch (utf_nextu2(&utf_ptr)) {
201 case 'V': fprintf (file, "void");
207 case 'Z': addoutputsize (4);
208 fprintf (file, "s4");
210 case 'J': addoutputsize (8);
211 fprintf (file, "s8");
213 case 'F': addoutputsize (4);
214 fprintf (file, "float");
216 case 'D': addoutputsize (8);
217 fprintf (file, "double");
220 addoutputsize ( sizeof(java_arrayheader*) );
221 switch (utf_nextu2(&utf_ptr)) {
222 case 'I': fprintf (file, "java_intarray*"); break;
223 case 'J': fprintf (file, "java_longarray*"); break;
224 case 'Z': fprintf (file, "java_booleanarray*"); break;
225 case 'B': fprintf (file, "java_bytearray*"); break;
226 case 'S': fprintf (file, "java_shortarray*"); break;
227 case 'C': fprintf (file, "java_chararray*"); break;
228 case 'F': fprintf (file, "java_floatarray*"); break;
229 case 'D': fprintf (file, "java_doublearray*"); break;
231 case '[': fprintf (file, "java_objectarray*");
232 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
234 while (utf_nextu2(&utf_ptr) != ';');
237 case 'L': fprintf (file, "java_objectarray*");
238 while ( utf_nextu2(&utf_ptr) != ';');
240 default: panic ("invalid type descriptor");
245 addoutputsize ( sizeof(java_objectheader*));
246 fprintf (file, "struct ");
247 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
251 default: panic ("Unknown type in field descriptor");
258 /***** determine the number of entries of a utf string in the ident chain *****/
260 static int searchidentchain_utf(utf *ident)
262 utf *u = chain_first(ident_chain); /* first element of list */
266 if (u==ident) count++; /* string found */
267 u = chain_next(ident_chain); /* next element in list */
274 /************** print structure for direct access to objects ******************/
276 static void printfields(classinfo *c)
283 addoutputsize(sizeof(java_objectheader));
284 fprintf(file, " java_objectheader header;\n");
288 printfields(c->super);
290 for (i = 0; i < c->fieldscount; i++) {
293 if (!(f->flags & ACC_STATIC)) {
295 printtype(f->descriptor->text);
297 utf_fprint(file, f->name);
299 /* rename multiple fieldnames */
300 if ((ident_count = searchidentchain_utf(f->name)))
301 fprintf(file, "%d", ident_count - 1);
302 chain_addlast(ident_chain, f->name);
304 fprintf(file, ";\n");
310 /***************** store prototype for native method in file ******************/
312 static void printmethod(methodinfo *m)
318 /* search for return-type in descriptor */
319 utf_ptr = m->descriptor->text;
320 while (utf_nextu2(&utf_ptr) != ')');
323 fprintf(file, "\n/*\n * Class: ");
324 utf_fprint(file, m->class->name);
325 fprintf(file, "\n * Method: ");
326 utf_fprint(file, m->name);
327 fprintf(file, "\n * Signature: ");
328 utf_fprint(file, m->descriptor);
329 fprintf(file, "\n */\n");
331 /* create prototype */
332 fprintf(file, "JNIEXPORT ");
334 fprintf(file, " JNICALL Java_");
335 printID(m->class->name);
337 /* rename overloaded method */
338 if ((ident_count = searchidentchain_utf(m->name)))
339 fprintf(file, "%d", ident_count - 1);
340 chain_addlast(ident_chain, m->name);
344 fprintf(file, "(JNIEnv *env");
346 utf_ptr = m->descriptor->text + 1;
348 if (!(m->flags & ACC_STATIC)) {
349 fprintf(file, ", struct ");
350 printID(m->class->name);
351 fprintf(file, "* this");
354 fprintf(file, ", jclass clazz");
357 if ((*utf_ptr) != ')') fprintf(file, ", ");
359 while ((*utf_ptr) != ')') {
360 utf_ptr = printtype(utf_ptr);
361 fprintf(file, " par%d", paramnum++);
362 if ((*utf_ptr)!=')') fprintf(file, ", ");
365 fprintf(file, ");\n\n");
369 /******* remove package-name in fully-qualified classname *********************/
371 static void gen_header_filename(char *buffer, utf *u)
375 for (i = 0;i < utf_strlen(u); i++) {
376 if ((u->text[i] == '/') || (u->text[i] == '$')) buffer[i] = '_'; /* convert '$' and '/' to '_' */
377 else buffer[i] = u->text[i];
379 buffer[utf_strlen(u)] = '\0';
383 /* create headerfile for classes and store native methods in chain ************/
385 static void headerfile_generate(classinfo *c)
387 char header_filename[1024] = "";
388 char classname[1024];
389 char uclassname[1024];
393 /* store class in chain */
394 chain_addlast(nativeclass_chain, c);
396 /* open headerfile for class */
397 gen_header_filename(classname, c->name);
399 /* create chain for renaming fields */
400 ident_chain = chain_new();
402 sprintf(header_filename, "nat/%s.h", classname);
403 file = fopen(header_filename, "w");
404 if (!file) panic("Can not open file to store header information");
406 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
408 /* convert to uppercase */
409 for (i = 0; classname[i]; i++) {
410 uclassname[i] = toupper(classname[i]);
412 uclassname[i] = '\0';
414 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
416 /* create structure for direct access to objects */
417 fprintf(file, "/* Structure information for class: ");
418 utf_fprint(file, c->name);
419 fprintf(file, " */\n\n");
420 fprintf(file, "typedef struct ");
422 fprintf(file, " {\n");
428 fprintf(file, ";\n\n");
430 /* create chain for renaming overloaded methods */
431 chain_free(ident_chain);
432 ident_chain = chain_new();
434 /* create method-prototypes */
436 for (i = 0; i < c->methodscount; i++) {
438 m = &(c->methods[i]);
440 if (m->flags & ACC_NATIVE) {
441 chain_addlast(nativemethod_chain, m);
446 chain_free(ident_chain);
448 fprintf(file, "#endif\n\n");
454 /******** print classname, '$' used to seperate inner-class name ***********/
456 void print_classname(classinfo *clazz)
458 utf *u = clazz->name;
459 char *endpos = u->text + u->blength;
460 char *utf_ptr = u->text;
463 while (utf_ptr < endpos) {
464 if ((c = utf_nextu2(&utf_ptr)) == '_') {
474 /*************** create table for locating native functions ****************/
476 static void printnativetableentry(methodinfo *m)
478 fprintf(file, " { \"");
479 print_classname(m->class);
480 fprintf(file, "\",\n \"");
481 utf_fprint(file, m->name);
482 fprintf(file, "\",\n \"");
483 utf_fprint(file, m->descriptor);
484 fprintf(file, "\",\n ");
486 if ((m->flags & ACC_STATIC) != 0)
487 fprintf(file, "true");
489 fprintf(file, "false");
491 fprintf(file, ",\n ");
492 fprintf(file, "(functionptr) Java_");
493 printID(m->class->name);
496 fprintf(file,"\n },\n");
500 /***************************************************************************
502 create the nativetypes-headerfile which includes
503 the headerfiles of the classes stored in the classes-chain
505 ****************************************************************************/
507 static void headers_finish()
511 char classname[1024];
513 file = fopen("nativetypes.hh", "w");
516 panic("Can not open file 'native.h' to store header information");
518 fprintf(file, "/* Headerfile for native methods: nativetypes.hh */\n");
519 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
520 fprintf(file, "\n/* include native-Headerfiles */\n\n");
522 c = chain_first(nativeclass_chain);
525 gen_header_filename(classname, c->name);
526 fprintf(file, "#include \"nat/%s.h\"\n", classname);
527 c = chain_next(nativeclass_chain);
531 chain_free(nativeclass_chain);
533 /* create table of native-methods */
535 file = fopen("nativetable.hh", "w");
538 panic("Can not open file 'nativetable' to store native-link-table");
540 fprintf(file, "/* Table of native methods: nativetables.hh */\n");
541 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
543 while ((m = chain_first(nativemethod_chain)) != NULL) {
544 chain_remove(nativemethod_chain);
545 printnativetableentry(m);
548 chain_free(nativemethod_chain);
553 /******************** internal function: print_usage ************************
555 Prints usage information for the JAVA header generator to stdout.
557 ***************************************************************************/
559 static void print_usage()
561 printf("Usage: cacaoh class [class..]\n");
566 /************************** Function: main *******************************
570 **************************************************************************/
572 int main(int argc, char **argv)
580 /********** internal (only used by main) *****************************/
582 char classpath[500] = "";
583 char offsets_filename[1024] = ""; /* phil */
584 u4 heapsize = 100000;
586 /*********** options so only headers are generated *******************/
588 makeinitializations = false;
591 /************ Collect some info from the environment *****************/
593 cp = getenv("CLASSPATH");
595 strcpy(classpath + strlen(classpath), ":");
596 strcpy(classpath + strlen(classpath), cp);
605 /**************************** Program start **************************/
608 log_text("Java - header-generator started");
610 sprintf(offsets_filename, "jit/%s/offsets.h", ARCH_DIR);
611 file = fopen(offsets_filename, "w");
613 fprintf(stderr, "Can not open file '%s' for write", offsets_filename);
617 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
619 fprintf(file, "#define offobjvftbl %3d\n", (int) OFFSET(java_objectheader, vftbl));
620 fprintf(file, "#define offarraysize %3d\n", (int) OFFSET(java_arrayheader, size));
621 fprintf(file, "#define offobjarrdata %3d\n\n", (int) OFFSET(java_objectarray, data[0]));
622 fprintf(file, "#define offbaseval %3d\n", (int) OFFSET(vftbl, baseval));
623 fprintf(file, "#define offdiffval %3d\n\n", (int) OFFSET(vftbl, diffval));
625 fprintf(file, "#define offclassvftbl %3d\n", (int) OFFSET(classinfo, vftbl));
626 fprintf(file, "#define offclassinit %3d\n\n", (int) OFFSET(classinfo, initialized));
628 fprintf(file, "#define offjniitemtype %3d\n", (int) OFFSET(jni_callblock, itemtype));
629 fprintf(file, "#define offjniitem %3d\n", (int) OFFSET(jni_callblock, item));
630 fprintf(file, "#define sizejniblock %3d\n\n", (int) sizeof(jni_callblock));
635 suck_init(classpath);
638 heap_init(heapsize, heapsize, &dummy);
642 /*********************** Load JAVA classes **************************/
644 nativemethod_chain = chain_new();
645 nativeclass_chain = chain_new();
647 for (a = 1; a < argc; a++) {
650 /* convert classname */
651 for (i = strlen(cp) - 1; i >= 0; i--) {
659 topclass = loader_load(utf_new_char(cp));
661 headerfile_generate(topclass);
666 /************************ Release all resources **********************/
670 tables_close(literalstring_free);
673 /* Print "finished" message */
675 log_text("Java - header-generator stopped");
683 void setVMClassField(classinfo *c)
688 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this) {
694 * These are local overrides for various environment variables in Emacs.
695 * Please do not remove this and leave it at the end of the file, where
696 * Emacs will automagically detect them.
697 * ---------------------------------------------------------------------
700 * indent-tabs-mode: t