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 771 2003-12-13 23:11:08Z stefan $
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 */
64 bool showmethods = false;
65 bool showconstantpool = false;
68 bool makeinitializations = true;
70 bool getloadingtime = false;
74 /******* replace some external functions *********/
76 functionptr native_findfunction(utf *cname, utf *mname, utf *desc, bool isstatic)
79 java_objectheader *javastring_new(utf *text)
83 void throw_noclassdeffounderror_message(utf* classname)
85 printf("Class not found: ");
86 utf_display(classname);
92 /* this is here to avoid link errors.
93 We are not interested in linkagerrors in cacaoh right now
95 void throw_linkageerror_message(utf* classname)
100 java_objectheader *literalstring_new(utf *u) { return NULL; }
103 void literalstring_free(java_objectheader *o) { }
104 void stringtable_update() { }
105 void synchronize_caches() { }
106 void asm_call_jit_compiler() { }
107 void asm_calljavafunction() { }
108 void asm_dumpregistersandcall() { }
109 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
111 s4 asm_builtin_idiv(s4 a, s4 b) {return 0;}
112 s4 asm_builtin_irem(s4 a, s4 b) {return 0;}
113 s8 asm_builtin_ldiv(s8 a, s8 b) {return 0;}
114 s8 asm_builtin_lrem(s8 a, s8 b) {return 0;}
116 s4 asm_builtin_f2i(float a) { return 0; }
117 s8 asm_builtin_f2l(float a) { return 0; }
118 s4 asm_builtin_d2i(double a) { return 0; }
119 s8 asm_builtin_d2l(double a) { return 0; }
121 void asm_builtin_monitorenter(java_objectheader *o) {}
122 void asm_builtin_monitorexit(java_objectheader *o) {}
124 s4 asm_builtin_checkarraycast(java_objectheader *o,arraydescriptor *d) {return 0;}
126 #if defined(__I386__)
127 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) { return 0; }
128 void asm_builtin_newarray(s4 size, vftbl *arrayvftbl) {}
131 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
133 u1 *createcompilerstub(methodinfo *m) {return NULL;}
134 u1 *createnativestub(functionptr f, methodinfo *m) {return NULL;}
135 u1 *oldcreatenativestub(functionptr f, methodinfo *m) {return NULL;}
137 void removecompilerstub(u1 *stub) {}
138 void removenativestub(u1 *stub) {}
140 void asm_perform_threadswitch(u1 **from, u1 **to) {}
141 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
142 void asm_switchstackandcall() {}
143 void asm_handle_builtin_exception(classinfo *c) {}
145 java_objectheader *native_new_and_init(void *p) { return NULL; }
148 /************************ global variables **********************/
150 java_objectheader *exceptionptr;
152 static chain *nativemethod_chain; /* chain with native methods */
153 static chain *nativeclass_chain; /* chain with processed classes */
154 static chain *ident_chain; /* chain with method and field names in current class */
155 static FILE *file = NULL;
156 static u4 outputsize;
157 static bool dopadding;
160 static void printIDpart(int c)
162 if ((c >= 'a' && c <= 'z')
163 || (c >= 'A' && c <= 'Z')
164 || (c >= '0' && c <= '9')
172 static void printID(utf *u)
174 char *utf_ptr = u->text;
177 for (i = 0; i < utf_strlen(u); i++)
178 printIDpart(utf_nextu2(&utf_ptr));
182 static void addoutputsize (int len)
185 if (!dopadding) return;
187 newsize = ALIGN(outputsize, len);
189 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
190 outputsize = newsize;
194 static char *printtype(char *utf_ptr)
198 switch (utf_nextu2(&utf_ptr)) {
199 case 'V': fprintf (file, "void");
205 case 'Z': addoutputsize (4);
206 fprintf (file, "s4");
208 case 'J': addoutputsize (8);
209 fprintf (file, "s8");
211 case 'F': addoutputsize (4);
212 fprintf (file, "float");
214 case 'D': addoutputsize (8);
215 fprintf (file, "double");
218 addoutputsize ( sizeof(java_arrayheader*) );
219 switch (utf_nextu2(&utf_ptr)) {
220 case 'I': fprintf (file, "java_intarray*"); break;
221 case 'J': fprintf (file, "java_longarray*"); break;
222 case 'Z': fprintf (file, "java_booleanarray*"); break;
223 case 'B': fprintf (file, "java_bytearray*"); break;
224 case 'S': fprintf (file, "java_shortarray*"); break;
225 case 'C': fprintf (file, "java_chararray*"); break;
226 case 'F': fprintf (file, "java_floatarray*"); break;
227 case 'D': fprintf (file, "java_doublearray*"); break;
229 case '[': fprintf (file, "java_objectarray*");
230 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
232 while (utf_nextu2(&utf_ptr) != ';');
235 case 'L': fprintf (file, "java_objectarray*");
236 while ( utf_nextu2(&utf_ptr) != ';');
238 default: panic ("invalid type descriptor");
243 addoutputsize ( sizeof(java_objectheader*));
244 fprintf (file, "struct ");
245 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
249 default: panic ("Unknown type in field descriptor");
256 /******* determine the number of entries of a utf string in the ident chain *****/
258 static int searchidentchain_utf(utf *ident)
260 utf *u = chain_first(ident_chain); /* first element of list */
264 if (u==ident) count++; /* string found */
265 u = chain_next(ident_chain); /* next element in list */
272 /**************** print structure for direct access to objects ******************/
274 static void printfields(classinfo *c)
281 addoutputsize(sizeof(java_objectheader));
282 fprintf(file, " java_objectheader header;\n");
286 printfields(c->super);
288 for (i = 0; i < c->fieldscount; i++) {
291 if (!(f->flags & ACC_STATIC)) {
293 printtype(f->descriptor->text);
295 utf_fprint(file, f->name);
297 /* rename multiple fieldnames */
298 if ((ident_count = searchidentchain_utf(f->name)))
299 fprintf(file, "%d", ident_count - 1);
300 chain_addlast(ident_chain, f->name);
302 fprintf(file, ";\n");
308 /***************** store prototype for native method in file ******************/
310 static void printmethod(methodinfo *m)
316 /* search for return-type in descriptor */
317 utf_ptr = m->descriptor->text;
318 while (utf_nextu2(&utf_ptr) != ')');
321 fprintf(file, "\n/*\n * Class: ");
322 utf_fprint(file, m->class->name);
323 fprintf(file, "\n * Method: ");
324 utf_fprint(file, m->name);
325 fprintf(file, "\n * Signature: ");
326 utf_fprint(file, m->descriptor);
327 fprintf(file, "\n */\n");
329 /* create prototype */
330 fprintf(file, "JNIEXPORT ");
332 fprintf(file, " JNICALL Java_");
333 printID(m->class->name);
335 /* rename overloaded method */
336 if ((ident_count = searchidentchain_utf(m->name)))
337 fprintf(file, "%d", ident_count - 1);
338 chain_addlast(ident_chain, m->name);
342 fprintf(file, "(JNIEnv *env");
344 utf_ptr = m->descriptor->text + 1;
346 if (!(m->flags & ACC_STATIC)) {
347 fprintf(file, ", struct ");
348 printID(m->class->name);
349 fprintf(file, "* this");
352 fprintf(file, ", jclass clazz");
355 if ((*utf_ptr) != ')') fprintf(file, ", ");
357 while ((*utf_ptr) != ')') {
358 utf_ptr = printtype(utf_ptr);
359 fprintf(file, " par%d", paramnum++);
360 if ((*utf_ptr)!=')') fprintf(file, ", ");
363 fprintf(file, ");\n\n");
367 /****************** remove package-name in fully-qualified classname *********************/
369 static void gen_header_filename(char *buffer, utf *u)
373 for (i = 0;i < utf_strlen(u); i++) {
374 if ((u->text[i] == '/') || (u->text[i] == '$')) buffer[i] = '_'; /* convert '$' and '/' to '_' */
375 else buffer[i] = u->text[i];
377 buffer[utf_strlen(u)] = '\0';
381 /*********** create headerfile for classes and store native methods in chain ************/
383 static void headerfile_generate(classinfo *c)
385 char header_filename[1024] = "";
386 char classname[1024];
387 char uclassname[1024];
391 /* store class in chain */
392 chain_addlast(nativeclass_chain, c);
394 /* open headerfile for class */
395 gen_header_filename(classname, c->name);
397 /* create chain for renaming fields */
398 ident_chain = chain_new();
400 sprintf(header_filename, "nat/%s.h", classname);
401 file = fopen(header_filename, "w");
402 if (!file) panic("Can not open file to store header information");
404 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
406 /* convert to uppercase */
407 for (i = 0; classname[i]; i++) {
408 uclassname[i] = toupper(classname[i]);
410 uclassname[i] = '\0';
412 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
414 /* create structure for direct access to objects */
415 fprintf(file, "/* Structure information for class: ");
416 utf_fprint(file, c->name);
417 fprintf(file, " */\n\n");
418 fprintf(file, "typedef struct ");
420 fprintf(file, " {\n");
426 fprintf(file, ";\n\n");
428 /* create chain for renaming overloaded methods */
429 chain_free(ident_chain);
430 ident_chain = chain_new();
432 /* create method-prototypes */
434 for (i = 0; i < c->methodscount; i++) {
436 m = &(c->methods[i]);
438 if (m->flags & ACC_NATIVE) {
439 chain_addlast(nativemethod_chain, m);
444 chain_free(ident_chain);
446 fprintf(file, "#endif\n\n");
452 /******** print classname, '$' used to seperate inner-class name ***********/
454 void print_classname(classinfo *clazz)
456 utf *u = clazz->name;
457 char *endpos = u->text + u->blength;
458 char *utf_ptr = u->text;
461 while (utf_ptr < endpos) {
462 if ((c = utf_nextu2(&utf_ptr)) == '_') {
472 /*************** create table for locating native functions ****************/
474 static void printnativetableentry(methodinfo *m)
476 fprintf(file, " { \"");
477 print_classname(m->class);
478 fprintf(file, "\",\n \"");
479 utf_fprint(file, m->name);
480 fprintf(file, "\",\n \"");
481 utf_fprint(file, m->descriptor);
482 fprintf(file, "\",\n ");
484 if ((m->flags & ACC_STATIC) != 0)
485 fprintf(file, "true");
487 fprintf(file, "false");
489 fprintf(file, ",\n ");
490 fprintf(file, "(functionptr) Java_");
491 printID(m->class->name);
494 fprintf(file,"\n },\n");
498 /***************************************************************************
500 create the nativetypes-headerfile which includes
501 the headerfiles of the classes stored in the classes-chain
503 ****************************************************************************/
505 static void headers_finish()
509 char classname[1024];
511 file = fopen("nativetypes.hh", "w");
514 panic("Can not open file 'native.h' to store header information");
516 fprintf(file, "/* Headerfile for native methods: nativetypes.hh */\n");
517 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
518 fprintf(file, "\n/* include native-Headerfiles */\n\n");
520 c = chain_first(nativeclass_chain);
523 gen_header_filename(classname, c->name);
524 fprintf(file, "#include \"nat/%s.h\"\n", classname);
525 c = chain_next(nativeclass_chain);
529 chain_free(nativeclass_chain);
531 /* create table of native-methods */
533 file = fopen("nativetable.hh", "w");
536 panic("Can not open file 'nativetable' to store native-link-table");
538 fprintf(file, "/* Table of native methods: nativetables.hh */\n");
539 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
541 while ((m = chain_first(nativemethod_chain)) != NULL) {
542 chain_remove(nativemethod_chain);
543 printnativetableentry(m);
546 chain_free(nativemethod_chain);
551 /******************** internal function: print_usage ************************
553 Prints usage information for the JAVA header generator to stdout.
555 ***************************************************************************/
557 static void print_usage()
559 printf("Usage: cacaoh class [class..]\n");
564 /************************** Function: main *******************************
568 **************************************************************************/
570 int main(int argc, char **argv)
578 /********** internal (only used by main) *****************************/
580 char classpath[500] = "";
581 char offsets_filename[1024] = ""; /* phil */
582 u4 heapsize = 100000;
584 /*********** options so only headers are generated *******************/
586 makeinitializations = false;
589 /************ Collect some info from the environment *****************/
591 cp = getenv("CLASSPATH");
593 strcpy(classpath + strlen(classpath), ":");
594 strcpy(classpath + strlen(classpath), cp);
603 /**************************** Program start **************************/
606 log_text("Java - header-generator started");
608 sprintf(offsets_filename, "jit/%s/offsets.h", ARCH_DIR);
609 file = fopen(offsets_filename, "w");
611 fprintf(stderr, "Can not open file '%s' for write", offsets_filename);
615 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
617 fprintf(file, "#define offobjvftbl %3d\n", (int) OFFSET(java_objectheader, vftbl));
618 fprintf(file, "#define offarraysize %3d\n", (int) OFFSET(java_arrayheader, size));
619 fprintf(file, "#define offobjarrdata %3d\n\n", (int) OFFSET(java_objectarray, data[0]));
620 fprintf(file, "#define offbaseval %3d\n", (int) OFFSET(vftbl, baseval));
621 fprintf(file, "#define offdiffval %3d\n\n", (int) OFFSET(vftbl, diffval));
623 fprintf(file, "#define offclassvftbl %3d\n", (int) OFFSET(classinfo, vftbl));
625 fprintf(file, "#define offjniitemtype %3d\n", (int) OFFSET(jni_callblock, itemtype));
626 fprintf(file, "#define offjniitem %3d\n", (int) OFFSET(jni_callblock, item));
627 fprintf(file, "#define sizejniblock %3d\n\n", (int) sizeof(jni_callblock));
632 suck_init(classpath);
635 heap_init(heapsize, heapsize, &dummy);
639 /*********************** Load JAVA classes **************************/
641 nativemethod_chain = chain_new();
642 nativeclass_chain = chain_new();
644 for (a = 1; a < argc; a++) {
647 /* convert classname */
648 for (i = strlen(cp) - 1; i >= 0; i--) {
656 topclass = loader_load(utf_new_char(cp));
658 headerfile_generate(topclass);
663 /************************ Release all resources **********************/
667 tables_close(literalstring_free);
670 /* Print "finished" message */
672 log_text("Java - header-generator stopped");
680 void setVMClassField(classinfo *c)
685 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this) {
691 * These are local overrides for various environment variables in Emacs.
692 * Please do not remove this and leave it at the end of the file, where
693 * Emacs will automagically detect them.
694 * ---------------------------------------------------------------------
697 * indent-tabs-mode: t