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 583 2003-11-09 19:14:31Z twisti $
43 #include "toolbox/chain.h"
44 #include "toolbox/memory.h"
45 #include "toolbox/loging.h"
48 /******* replace some external functions *********/
50 functionptr native_findfunction (utf *cname, utf *mname, utf *desc, bool isstatic)
53 java_objectheader *javastring_new (utf *text) /* schani */
56 void throw_classnotfoundexception()
58 panic("class not found");
61 java_objectheader *literalstring_new (utf *u)
64 void literalstring_free(java_objectheader *o) { }
65 void stringtable_update() { }
66 void synchronize_caches() { }
67 void asm_call_jit_compiler() { }
68 void asm_calljavamethod() { }
69 void asm_dumpregistersandcall() { }
70 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
72 s4 asm_builtin_idiv(s4 a, s4 b) {return 0;}
73 s4 asm_builtin_irem(s4 a, s4 b) {return 0;}
74 s8 asm_builtin_ldiv(s8 a, s8 b) {return 0;}
75 s8 asm_builtin_lrem(s8 a, s8 b) {return 0;}
77 s4 asm_builtin_f2i(float a) { return 0; }
78 s8 asm_builtin_f2l(float a) { return 0; }
79 s4 asm_builtin_d2i(double a) { return 0; }
80 s8 asm_builtin_d2l(double a) { return 0; }
82 void asm_builtin_monitorenter(java_objectheader *o) {}
83 void asm_builtin_monitorexit(java_objectheader *o) {}
85 s4 asm_builtin_checkarraycast(java_objectheader *o, constant_arraydescriptor *d) {return 0;}
88 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) { return 0; }
89 void asm_builtin_anewarray(s4 size, classinfo *elementtype) {}
90 void asm_builtin_newarray_array(s4 size, constant_arraydescriptor *elementdesc) {}
92 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
94 u1 *createcompilerstub(methodinfo *m) {return NULL;}
95 u1 *createnativestub(functionptr f, methodinfo *m) {return NULL;}
96 u1 *oldcreatenativestub(functionptr f, methodinfo *m) {return NULL;}
98 void removecompilerstub(u1 *stub) {}
99 void removenativestub(u1 *stub) {}
101 void asm_perform_threadswitch(u1 **from, u1 **to) {}
102 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
103 void asm_switchstackandcall() {}
104 void asm_handle_builtin_exception(classinfo *c) {}
106 java_objectheader *native_new_and_init(void *p) { return NULL; }
108 /************************ global variables **********************/
110 java_objectheader *exceptionptr; /* schani */
112 bool verbose = false;
113 bool runverbose = false;
114 bool collectverbose = false;
116 static chain *nativemethod_chain; /* chain with native methods */
117 static chain *nativeclass_chain; /* chain with processed classes */
118 static chain *ident_chain; /* chain with method and field names in current class */
119 static FILE *file = NULL;
120 static u4 outputsize;
121 static bool dopadding;
124 static void printIDpart(int c)
126 if ((c >= 'a' && c <= 'z')
127 || (c >= 'A' && c <= 'Z')
128 || (c >= '0' && c <= '9')
136 static void printID(utf *u)
138 char *utf_ptr = u->text;
141 for (i = 0; i < utf_strlen(u); i++)
142 printIDpart(utf_nextu2(&utf_ptr));
146 static void addoutputsize (int len)
149 if (!dopadding) return;
151 newsize = ALIGN(outputsize, len);
153 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
154 outputsize = newsize;
158 static char *printtype (char *utf_ptr)
162 switch (utf_nextu2(&utf_ptr)) {
163 case 'V': fprintf (file, "void");
169 case 'Z': addoutputsize (4);
170 fprintf (file, "s4");
172 case 'J': addoutputsize (8);
173 fprintf (file, "s8");
175 case 'F': addoutputsize (4);
176 fprintf (file, "float");
178 case 'D': addoutputsize (8);
179 fprintf (file, "double");
182 addoutputsize ( sizeof(java_arrayheader*) );
183 switch (utf_nextu2(&utf_ptr)) {
184 case 'I': fprintf (file, "java_intarray*"); break;
185 case 'J': fprintf (file, "java_longarray*"); break;
186 case 'Z': fprintf (file, "java_booleanarray*"); break;
187 case 'B': fprintf (file, "java_bytearray*"); break;
188 case 'S': fprintf (file, "java_shortarray*"); break;
189 case 'C': fprintf (file, "java_chararray*"); break;
190 case 'F': fprintf (file, "java_floatarray*"); break;
191 case 'D': fprintf (file, "java_doublearray*"); break;
193 case '[': fprintf (file, "java_arrayarray*");
194 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
196 while (utf_nextu2(&utf_ptr) != ';');
199 case 'L': fprintf (file, "java_objectarray*");
200 while ( utf_nextu2(&utf_ptr) != ';');
202 default: panic ("invalid type descriptor");
207 addoutputsize ( sizeof(java_objectheader*));
208 fprintf (file, "struct ");
209 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
213 default: panic ("Unknown type in field descriptor");
219 /******* determine the number of entries of a utf string in the ident chain *****/
221 static int searchidentchain_utf(utf *ident)
223 utf *u = chain_first(ident_chain); /* first element of list */
227 if (u==ident) count++; /* string found */
228 u = chain_next(ident_chain); /* next element in list */
234 /**************** print structure for direct access to objects ******************/
236 static void printfields (classinfo *c)
243 addoutputsize ( sizeof(java_objectheader) );
244 fprintf (file, " java_objectheader header;\n");
248 printfields (c->super);
250 for (i=0; i<c->fieldscount; i++) {
253 if (! (f->flags & ACC_STATIC) ) {
255 printtype (f->descriptor->text);
257 utf_fprint (file, f->name);
259 /* rename multiple fieldnames */
260 if ((ident_count = searchidentchain_utf(f->name)))
261 fprintf(file,"%d",ident_count - 1);
262 chain_addlast(ident_chain,f->name);
264 fprintf (file, ";\n");
269 /***************** store prototype for native method in file ******************/
271 static void printmethod (methodinfo *m)
277 /* search for return-type in descriptor */
278 utf_ptr = m->descriptor->text;
279 while (utf_nextu2(&utf_ptr) != ')');
282 fprintf (file,"/*\n * Class: ");
283 utf_fprint (file, m->class->name);
284 fprintf (file,"\n * Method: ");
285 utf_fprint (file, m->name);
286 fprintf (file,"\n * Signature: ");
287 utf_fprint (file, m->descriptor);
288 fprintf (file,"\n */\n");
290 /* create prototype */
291 fprintf (file,"JNIEXPORT ");
293 fprintf (file," JNICALL Java_");
294 printID (m->class->name);
296 /* rename overloaded method */
297 if ((ident_count = searchidentchain_utf(m->name)))
298 fprintf(file,"%d",ident_count - 1);
299 chain_addlast(ident_chain,m->name);
303 fprintf (file," (JNIEnv *env ");
305 utf_ptr = m->descriptor->text+1;
307 if (! (m->flags & ACC_STATIC) ) {
309 fprintf (file, ", struct ");
310 printID (m->class->name);
311 fprintf (file, "* this ");
315 if ((*utf_ptr)!=')') fprintf (file, ", ");
317 while ((*utf_ptr)!=')') {
318 utf_ptr = printtype (utf_ptr);
319 fprintf (file, " par%d", paramnum++);
320 if ((*utf_ptr)!=')') fprintf (file, ", ");
323 fprintf (file, ");\n");
327 /****************** remove package-name in fully-qualified classname *********************/
329 static void simple_classname(char *buffer, utf *u)
331 int i, simplename_start;
333 for (i=utf_strlen(u)-1; i>=0; i--) {
335 if (u->text[i] == '$') u->text[i] = '_'; else /* convert '$' to '_' */
336 if (u->text[i] == '/') {
337 /* beginning of simple name */
338 simplename_start = i+1;
343 for (i=simplename_start; i < utf_strlen(u); i++)
344 buffer[i-simplename_start] = u->text[i];
346 buffer[i-simplename_start] = '\0';
349 /*********** create headerfile for classes and store native methods in chain ************/
351 static void headerfile_generate (classinfo *c)
353 char header_filename[1024] = "";
354 char classname[1024];
358 /* store class in chain */
359 chain_addlast (nativeclass_chain, c);
361 /* open headerfile for class */
362 simple_classname(classname,c->name);
364 /* create chain for renaming fields */
365 ident_chain = chain_new ();
367 sprintf(header_filename, "nat/%s.h", classname);
368 file = fopen (header_filename, "w");
369 if (!file) panic ("Can not open file to store header information");
370 fprintf (file, "/* This file is machine generated, don't edit it !*/\n\n");
372 /* create structure for direct access to objects */
373 fprintf (file, "/* Structure information for class: ");
374 utf_fprint (file, c->name);
375 fprintf (file, " */\n\n");
376 fprintf (file, "typedef struct ");
378 fprintf (file, " {\n");
382 fprintf (file, "} ");
384 fprintf (file, ";\n\n");
386 /* create chain for renaming overloaded methods */
387 chain_free(ident_chain);
388 ident_chain = chain_new ();
390 /* create method-prototypes */
392 for (i=0; i<c->methodscount; i++) {
394 m = &(c->methods[i]);
396 if (m->flags & ACC_NATIVE) {
397 chain_addlast (nativemethod_chain, m);
403 chain_free(ident_chain);
407 /******** print classname, '$' used to seperate inner-class name ***********/
409 void print_classname (classinfo *clazz)
411 utf *u = clazz->name;
412 char *endpos = u->text + u->blength;
413 char *utf_ptr = u->text;
416 while (utf_ptr<endpos) {
417 if ((c=utf_nextu2(&utf_ptr)) == '_')
425 /*************** create table for locating native functions ****************/
427 static void printnativetableentry (methodinfo *m)
429 fprintf (file, " { \"");
430 print_classname(m->class);
431 fprintf (file, "\",\n \"");
432 utf_fprint (file, m->name);
433 fprintf (file, "\",\n \"");
434 utf_fprint (file, m->descriptor);
435 fprintf (file, "\",\n ");
436 if ( (m->flags & ACC_STATIC) !=0) fprintf (file, "true");
437 else fprintf (file, "false");
438 fprintf (file, ",\n ");
439 fprintf (file, "(functionptr) Java_");
440 printID (m->class->name);
443 fprintf (file,"\n },\n");
447 /***************************************************************************
449 create the nativetypes-headerfile which includes
450 the headerfiles of the classes stored in the classes-chain
452 ****************************************************************************/
454 static void headers_finish ()
458 char classname[1024];
460 file = fopen ("nativetypes.hh", "w");
461 if (!file) panic ("Can not open file 'native.h' to store header information");
463 fprintf (file, "/* Headerfile for native methods: nativetypes.hh */\n");
464 fprintf (file, "/* This file is machine generated, don't edit it !*/\n\n");
465 fprintf (file, "\n/* include native-Headerfiles */\n\n");
467 c = chain_first (nativeclass_chain);
471 simple_classname(classname,c->name);
472 fprintf(file,"#include \"nat/%s.h\"\n",classname);
473 c = chain_next (nativeclass_chain);
477 chain_free (nativeclass_chain);
479 /* create table of native-methods */
481 file = fopen ("nativetable.hh", "w");
482 if (!file) panic ("Can not open file 'nativetable' to store native-link-table");
484 fprintf (file, "/* Table of native methods: nativetables.hh */\n");
485 fprintf (file, "/* This file is machine generated, don't edit it !*/\n\n");
487 while ( (m = chain_first (nativemethod_chain)) != NULL) {
488 chain_remove (nativemethod_chain);
489 printnativetableentry (m);
492 chain_free (nativemethod_chain);
498 /******************** internal function: print_usage ************************
500 Prints usage information for the JAVA header generator to stdout.
502 ***************************************************************************/
504 static void print_usage()
506 printf("Usage: cacaoh class [class..]\n");
511 /************************** Function: main *******************************
515 **************************************************************************/
517 int main(int argc, char **argv)
525 /********** internal (only used by main) *****************************/
527 char classpath[500] = "";
528 char offsets_filename[1024] = ""; /* phil */
529 u4 heapsize = 100000;
531 /*********** options so only headers are generated *******************/
533 makeinitializations=false;
536 /************ Collect some info from the environment *****************/
538 cp = getenv("CLASSPATH");
540 strcpy(classpath + strlen(classpath), ":");
541 strcpy(classpath + strlen(classpath), cp);
550 /**************************** Program start **************************/
553 log_text("Java - header-generator started");
555 sprintf(offsets_filename, "jit/%s/offsets.h", ARCH_DIR); /* phil */
556 file = fopen(offsets_filename, "w");
558 fprintf(stderr, "Can not open file '%s' for write", offsets_filename);
562 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
564 fprintf(file, "#define offobjvftbl %3d\n", (int) OFFSET(java_objectheader, vftbl));
565 fprintf(file, "#define offarraysize %3d\n", (int) OFFSET(java_arrayheader, size));
566 fprintf(file, "#define offobjarrdata %3d\n\n", (int) OFFSET(java_objectarray, data[0]));
567 fprintf(file, "#define offbaseval %3d\n", (int) OFFSET(vftbl, baseval));
568 fprintf(file, "#define offdiffval %3d\n", (int) OFFSET(vftbl, diffval));
572 suck_init(classpath);
575 heap_init(heapsize, heapsize, &dummy);
579 /*********************** Load JAVA classes **************************/
581 nativemethod_chain = chain_new();
582 nativeclass_chain = chain_new();
584 for (a = 1; a < argc; a++) {
587 /* convert classname */
588 for (i = strlen(cp) - 1; i >= 0; i--)
595 topclass = loader_load(utf_new_char(cp));
597 headerfile_generate(topclass);
602 /************************ Release all resources **********************/
606 tables_close(literalstring_free);
609 /* Print "finished" message */
611 log_text("Java - header-generator stopped");
620 * These are local overrides for various environment variables in Emacs.
621 * Please do not remove this and leave it at the end of the file, where
622 * Emacs will automagically detect them.
623 * ---------------------------------------------------------------------
626 * indent-tabs-mode: t