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 557 2003-11-02 22:51:59Z twisti $
42 #include "toolbox/chain.h"
43 #include "toolbox/memory.h"
44 #include "toolbox/loging.h"
47 /******* replace some external functions *********/
49 functionptr native_findfunction (utf *cname, utf *mname, utf *desc, bool isstatic)
52 java_objectheader *javastring_new (utf *text) /* schani */
55 void throw_classnotfoundexception()
57 panic("class not found");
60 java_objectheader *literalstring_new (utf *u)
63 void literalstring_free (java_objectheader *o) { }
64 void stringtable_update () { }
65 void synchronize_caches() { }
66 void asm_call_jit_compiler () { }
67 void asm_calljavamethod () { }
68 void asm_dumpregistersandcall () { }
69 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
71 s4 asm_builtin_idiv (s4 a, s4 b) {return 0;}
72 s4 asm_builtin_irem (s4 a, s4 b) {return 0;}
73 s8 asm_builtin_ldiv (s8 a, s8 b) {return 0;}
74 s8 asm_builtin_lrem (s8 a, s8 b) {return 0;}
76 s4 asm_builtin_f2i (float a) { return 0; }
77 s8 asm_builtin_f2l (float a) { return 0; }
78 s4 asm_builtin_d2i (double a) { return 0; }
79 s8 asm_builtin_d2l (double a) { return 0; }
81 void asm_builtin_monitorenter (java_objectheader *o) {}
82 void asm_builtin_monitorexit (java_objectheader *o) {}
84 s4 asm_builtin_checkarraycast(java_objectheader *o, constant_arraydescriptor *d) {return 0;}
87 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) { return 0; }
88 void asm_builtin_anewarray (s4 size, classinfo *elementtype) {}
89 void asm_builtin_newarray_array (s4 size, constant_arraydescriptor *elementdesc) {}
91 void asm_builtin_aastore (java_objectarray *a, s4 index, java_objectheader *o) {}
93 u1 *createcompilerstub (methodinfo *m) {return NULL;}
94 u1 *createnativestub (functionptr f, methodinfo *m) {return NULL;}
95 u1 *oldcreatenativestub (functionptr f, methodinfo *m) {return NULL;}
97 void removecompilerstub (u1 *stub) {}
98 void removenativestub (u1 *stub) {}
100 void asm_perform_threadswitch (u1 **from, u1 **to) {}
101 u1* asm_initialize_thread_stack (void *func, u1 *stack) { return NULL; }
102 void asm_switchstackandcall () { }
104 java_objectheader *native_new_and_init (void *p) { return NULL; }
106 /************************ global variables **********************/
108 java_objectheader *exceptionptr; /* schani */
109 bool verbose = false;
111 static chain *nativemethod_chain; /* chain with native methods */
112 static chain *nativeclass_chain; /* chain with processed classes */
113 static chain *ident_chain; /* chain with method and field names in current class */
114 static FILE *file = NULL;
115 static u4 outputsize;
116 static bool dopadding;
118 static void printIDpart (int c)
120 if ( (c>='a' && c<='z')
121 || (c>='A' && c<='Z')
122 || (c>='0' && c<='9')
125 else putc ('_',file);
129 static void printID (utf *u)
131 char *utf_ptr = u->text;
134 for (i=0; i<utf_strlen(u); i++)
135 printIDpart (utf_nextu2(&utf_ptr));
138 static void addoutputsize (int len)
141 if (!dopadding) return;
143 newsize = ALIGN (outputsize, len);
145 for (i=outputsize; i<newsize; i++) fprintf (file, " u1 pad%d\n",(int) i);
146 outputsize = newsize;
150 static char *printtype (char *utf_ptr)
154 switch (utf_nextu2(&utf_ptr)) {
155 case 'V': fprintf (file, "void");
161 case 'Z': addoutputsize (4);
162 fprintf (file, "s4");
164 case 'J': addoutputsize (8);
165 fprintf (file, "s8");
167 case 'F': addoutputsize (4);
168 fprintf (file, "float");
170 case 'D': addoutputsize (8);
171 fprintf (file, "double");
174 addoutputsize ( sizeof(java_arrayheader*) );
175 switch (utf_nextu2(&utf_ptr)) {
176 case 'I': fprintf (file, "java_intarray*"); break;
177 case 'J': fprintf (file, "java_longarray*"); break;
178 case 'Z': fprintf (file, "java_booleanarray*"); break;
179 case 'B': fprintf (file, "java_bytearray*"); break;
180 case 'S': fprintf (file, "java_shortarray*"); break;
181 case 'C': fprintf (file, "java_chararray*"); break;
182 case 'F': fprintf (file, "java_floatarray*"); break;
183 case 'D': fprintf (file, "java_doublearray*"); break;
185 case '[': fprintf (file, "java_arrayarray*");
186 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
188 while (utf_nextu2(&utf_ptr) != ';');
191 case 'L': fprintf (file, "java_objectarray*");
192 while ( utf_nextu2(&utf_ptr) != ';');
194 default: panic ("invalid type descriptor");
199 addoutputsize ( sizeof(java_objectheader*));
200 fprintf (file, "struct ");
201 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
205 default: panic ("Unknown type in field descriptor");
211 /******* determine the number of entries of a utf string in the ident chain *****/
213 static int searchidentchain_utf(utf *ident)
215 utf *u = chain_first(ident_chain); /* first element of list */
219 if (u==ident) count++; /* string found */
220 u = chain_next(ident_chain); /* next element in list */
226 /**************** print structure for direct access to objects ******************/
228 static void printfields (classinfo *c)
235 addoutputsize ( sizeof(java_objectheader) );
236 fprintf (file, " java_objectheader header;\n");
240 printfields (c->super);
242 for (i=0; i<c->fieldscount; i++) {
245 if (! (f->flags & ACC_STATIC) ) {
247 printtype (f->descriptor->text);
249 utf_fprint (file, f->name);
251 /* rename multiple fieldnames */
252 if ((ident_count = searchidentchain_utf(f->name)))
253 fprintf(file,"%d",ident_count - 1);
254 chain_addlast(ident_chain,f->name);
256 fprintf (file, ";\n");
261 /***************** store prototype for native method in file ******************/
263 static void printmethod (methodinfo *m)
269 /* search for return-type in descriptor */
270 utf_ptr = m->descriptor->text;
271 while (utf_nextu2(&utf_ptr) != ')');
274 fprintf (file,"/*\n * Class: ");
275 utf_fprint (file, m->class->name);
276 fprintf (file,"\n * Method: ");
277 utf_fprint (file, m->name);
278 fprintf (file,"\n * Signature: ");
279 utf_fprint (file, m->descriptor);
280 fprintf (file,"\n */\n");
282 /* create prototype */
283 fprintf (file,"JNIEXPORT ");
285 fprintf (file," JNICALL Java_");
286 printID (m->class->name);
288 /* rename overloaded method */
289 if ((ident_count = searchidentchain_utf(m->name)))
290 fprintf(file,"%d",ident_count - 1);
291 chain_addlast(ident_chain,m->name);
295 fprintf (file," (JNIEnv *env ");
297 utf_ptr = m->descriptor->text+1;
299 if (! (m->flags & ACC_STATIC) ) {
301 fprintf (file, ", struct ");
302 printID (m->class->name);
303 fprintf (file, "* this ");
307 if ((*utf_ptr)!=')') fprintf (file, ", ");
309 while ((*utf_ptr)!=')') {
310 utf_ptr = printtype (utf_ptr);
311 fprintf (file, " par%d", paramnum++);
312 if ((*utf_ptr)!=')') fprintf (file, ", ");
315 fprintf (file, ");\n");
319 /****************** remove package-name in fully-qualified classname *********************/
321 static void simple_classname(char *buffer, utf *u)
323 int i, simplename_start;
325 for (i=utf_strlen(u)-1; i>=0; i--) {
327 if (u->text[i] == '$') u->text[i] = '_'; else /* convert '$' to '_' */
328 if (u->text[i] == '/') {
329 /* beginning of simple name */
330 simplename_start = i+1;
335 for (i=simplename_start; i < utf_strlen(u); i++)
336 buffer[i-simplename_start] = u->text[i];
338 buffer[i-simplename_start] = '\0';
341 /*********** create headerfile for classes and store native methods in chain ************/
343 static void headerfile_generate (classinfo *c)
345 char header_filename[1024] = "";
346 char classname[1024];
350 /* store class in chain */
351 chain_addlast (nativeclass_chain, c);
353 /* open headerfile for class */
354 simple_classname(classname,c->name);
356 /* create chain for renaming fields */
357 ident_chain = chain_new ();
359 sprintf(header_filename, "nat/%s.h", classname);
360 file = fopen (header_filename, "w");
361 if (!file) panic ("Can not open file to store header information");
362 fprintf (file, "/* This file is machine generated, don't edit it !*/\n\n");
364 /* create structure for direct access to objects */
365 fprintf (file, "/* Structure information for class: ");
366 utf_fprint (file, c->name);
367 fprintf (file, " */\n\n");
368 fprintf (file, "typedef struct ");
370 fprintf (file, " {\n");
374 fprintf (file, "} ");
376 fprintf (file, ";\n\n");
378 /* create chain for renaming overloaded methods */
379 chain_free(ident_chain);
380 ident_chain = chain_new ();
382 /* create method-prototypes */
384 for (i=0; i<c->methodscount; i++) {
386 m = &(c->methods[i]);
388 if (m->flags & ACC_NATIVE) {
389 chain_addlast (nativemethod_chain, m);
395 chain_free(ident_chain);
399 /******** print classname, '$' used to seperate inner-class name ***********/
401 void print_classname (classinfo *clazz)
403 utf *u = clazz->name;
404 char *endpos = u->text + u->blength;
405 char *utf_ptr = u->text;
408 while (utf_ptr<endpos) {
409 if ((c=utf_nextu2(&utf_ptr)) == '_')
417 /*************** create table for locating native functions ****************/
419 static void printnativetableentry (methodinfo *m)
421 fprintf (file, " { \"");
422 print_classname(m->class);
423 fprintf (file, "\",\n \"");
424 utf_fprint (file, m->name);
425 fprintf (file, "\",\n \"");
426 utf_fprint (file, m->descriptor);
427 fprintf (file, "\",\n ");
428 if ( (m->flags & ACC_STATIC) !=0) fprintf (file, "true");
429 else fprintf (file, "false");
430 fprintf (file, ",\n ");
431 fprintf (file, "(functionptr) Java_");
432 printID (m->class->name);
435 fprintf (file,"\n },\n");
439 /***************************************************************************
441 create the nativetypes-headerfile which includes
442 the headerfiles of the classes stored in the classes-chain
444 ****************************************************************************/
446 static void headers_finish ()
450 char classname[1024];
452 file = fopen ("nativetypes.hh", "w");
453 if (!file) panic ("Can not open file 'native.h' to store header information");
455 fprintf (file, "/* Headerfile for native methods: nativetypes.hh */\n");
456 fprintf (file, "/* This file is machine generated, don't edit it !*/\n\n");
457 fprintf (file, "\n/* include native-Headerfiles */\n\n");
459 c = chain_first (nativeclass_chain);
463 simple_classname(classname,c->name);
464 fprintf(file,"#include \"nat/%s.h\"\n",classname);
465 c = chain_next (nativeclass_chain);
469 chain_free (nativeclass_chain);
471 /* create table of native-methods */
473 file = fopen ("nativetable.hh", "w");
474 if (!file) panic ("Can not open file 'nativetable' to store native-link-table");
476 fprintf (file, "/* Table of native methods: nativetables.hh */\n");
477 fprintf (file, "/* This file is machine generated, don't edit it !*/\n\n");
479 while ( (m = chain_first (nativemethod_chain)) != NULL) {
480 chain_remove (nativemethod_chain);
481 printnativetableentry (m);
484 chain_free (nativemethod_chain);
490 /******************** internal function: print_usage ************************
492 Prints usage information for the JAVA header generator to stdout.
494 ***************************************************************************/
496 static void print_usage()
498 printf("Usage: cacaoh class [class..]\n");
503 /************************** Function: main *******************************
507 **************************************************************************/
509 int main(int argc, char **argv)
517 /********** internal (only used by main) *****************************/
519 char classpath[500] = "";
520 char offsets_filename[1024] = ""; /* phil */
521 u4 heapsize = 100000;
523 /*********** options so only headers are generated *******************/
525 makeinitializations=false;
528 /************ Collect some info from the environment *****************/
530 cp = getenv("CLASSPATH");
532 strcpy(classpath + strlen(classpath), ":");
533 strcpy(classpath + strlen(classpath), cp);
542 /**************************** Program start **************************/
545 log_text("Java - header-generator started");
547 sprintf(offsets_filename, "jit/%s/offsets.h", ARCH_DIR); /* phil */
548 file = fopen(offsets_filename, "w");
550 fprintf(stderr, "Can not open file '%s' for write", offsets_filename);
554 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
556 fprintf(file, "#define offobjvftbl %3d\n", (int) OFFSET(java_objectheader, vftbl));
557 fprintf(file, "#define offarraysize %3d\n", (int) OFFSET(java_arrayheader, size));
558 fprintf(file, "#define offobjarrdata %3d\n\n", (int) OFFSET(java_objectarray, data[0]));
559 fprintf(file, "#define offbaseval %3d\n", (int) OFFSET(vftbl, baseval));
560 fprintf(file, "#define offdiffval %3d\n", (int) OFFSET(vftbl, diffval));
564 suck_init(classpath);
567 heap_init(heapsize, heapsize, &dummy);
571 /*********************** Load JAVA classes **************************/
573 nativemethod_chain = chain_new();
574 nativeclass_chain = chain_new();
576 for (a = 1; a < argc; a++) {
579 /* convert classname */
580 for (i = strlen(cp) - 1; i >= 0; i--)
587 topclass = loader_load(utf_new_char(cp));
589 headerfile_generate(topclass);
594 /************************ Release all resources **********************/
598 tables_close( literalstring_free );
601 /* Print "finished" message */
603 log_text("Java - header-generator stopped");
612 * These are local overrides for various environment variables in Emacs.
613 * Please do not remove this and leave it at the end of the file, where
614 * Emacs will automagically detect them.
615 * ---------------------------------------------------------------------
618 * indent-tabs-mode: t