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 735 2003-12-13 15:47:47Z stefan $
44 #include "toolbox/chain.h"
45 #include "toolbox/memory.h"
46 #include "toolbox/loging.h"
49 /******* replace some external functions *********/
51 functionptr native_findfunction (utf *cname, utf *mname, utf *desc, bool isstatic)
54 java_objectheader *javastring_new (utf *text) /* schani */
57 void throw_classnotfoundexception()
59 panic("class not found----------");
63 void throw_classnotfoundexception2(utf* classname)
65 /* [stefan] code was useless
66 sprintf (logtext, "Loading class: ");
67 utf_sprint (logtext+strlen(logtext), classname);
69 panic("******class not found");
72 /* this is here to avoid link errors. We are not interested in linkagerrors in cacaoh right now*/
73 void throw_linkageerror2(utf* classname)
77 java_objectheader *literalstring_new (utf *u)
80 void literalstring_free(java_objectheader *o) { }
81 void stringtable_update() { }
82 void synchronize_caches() { }
83 void asm_call_jit_compiler() { }
84 void asm_calljavamethod() { }
85 void asm_dumpregistersandcall() { }
86 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
88 s4 asm_builtin_idiv(s4 a, s4 b) {return 0;}
89 s4 asm_builtin_irem(s4 a, s4 b) {return 0;}
90 s8 asm_builtin_ldiv(s8 a, s8 b) {return 0;}
91 s8 asm_builtin_lrem(s8 a, s8 b) {return 0;}
93 s4 asm_builtin_f2i(float a) { return 0; }
94 s8 asm_builtin_f2l(float a) { return 0; }
95 s4 asm_builtin_d2i(double a) { return 0; }
96 s8 asm_builtin_d2l(double a) { return 0; }
98 void asm_builtin_monitorenter(java_objectheader *o) {}
99 void asm_builtin_monitorexit(java_objectheader *o) {}
101 s4 asm_builtin_checkarraycast(java_objectheader *o,arraydescriptor *d) {return 0;}
103 #if defined(__I386__)
104 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) { return 0; }
105 void asm_builtin_newarray(s4 size, vftbl *arrayvftbl) {}
108 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
110 u1 *createcompilerstub(methodinfo *m) {return NULL;}
111 u1 *createnativestub(functionptr f, methodinfo *m) {return NULL;}
112 u1 *oldcreatenativestub(functionptr f, methodinfo *m) {return NULL;}
114 void removecompilerstub(u1 *stub) {}
115 void removenativestub(u1 *stub) {}
117 void asm_perform_threadswitch(u1 **from, u1 **to) {}
118 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
119 void asm_switchstackandcall() {}
120 void asm_handle_builtin_exception(classinfo *c) {}
122 java_objectheader *native_new_and_init(void *p) { return NULL; }
125 /************************ global variables **********************/
127 java_objectheader *exceptionptr;
129 bool verbose = false;
130 bool runverbose = false;
131 bool collectverbose = false;
133 static chain *nativemethod_chain; /* chain with native methods */
134 static chain *nativeclass_chain; /* chain with processed classes */
135 static chain *ident_chain; /* chain with method and field names in current class */
136 static FILE *file = NULL;
137 static u4 outputsize;
138 static bool dopadding;
141 static void printIDpart(int c)
143 if ((c >= 'a' && c <= 'z')
144 || (c >= 'A' && c <= 'Z')
145 || (c >= '0' && c <= '9')
153 static void printID(utf *u)
155 char *utf_ptr = u->text;
158 for (i = 0; i < utf_strlen(u); i++)
159 printIDpart(utf_nextu2(&utf_ptr));
163 static void addoutputsize (int len)
166 if (!dopadding) return;
168 newsize = ALIGN(outputsize, len);
170 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
171 outputsize = newsize;
175 static char *printtype(char *utf_ptr)
179 switch (utf_nextu2(&utf_ptr)) {
180 case 'V': fprintf (file, "void");
186 case 'Z': addoutputsize (4);
187 fprintf (file, "s4");
189 case 'J': addoutputsize (8);
190 fprintf (file, "s8");
192 case 'F': addoutputsize (4);
193 fprintf (file, "float");
195 case 'D': addoutputsize (8);
196 fprintf (file, "double");
199 addoutputsize ( sizeof(java_arrayheader*) );
200 switch (utf_nextu2(&utf_ptr)) {
201 case 'I': fprintf (file, "java_intarray*"); break;
202 case 'J': fprintf (file, "java_longarray*"); break;
203 case 'Z': fprintf (file, "java_booleanarray*"); break;
204 case 'B': fprintf (file, "java_bytearray*"); break;
205 case 'S': fprintf (file, "java_shortarray*"); break;
206 case 'C': fprintf (file, "java_chararray*"); break;
207 case 'F': fprintf (file, "java_floatarray*"); break;
208 case 'D': fprintf (file, "java_doublearray*"); break;
210 case '[': fprintf (file, "java_objectarray*");
211 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
213 while (utf_nextu2(&utf_ptr) != ';');
216 case 'L': fprintf (file, "java_objectarray*");
217 while ( utf_nextu2(&utf_ptr) != ';');
219 default: panic ("invalid type descriptor");
224 addoutputsize ( sizeof(java_objectheader*));
225 fprintf (file, "struct ");
226 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
230 default: panic ("Unknown type in field descriptor");
237 /******* determine the number of entries of a utf string in the ident chain *****/
239 static int searchidentchain_utf(utf *ident)
241 utf *u = chain_first(ident_chain); /* first element of list */
245 if (u==ident) count++; /* string found */
246 u = chain_next(ident_chain); /* next element in list */
253 /**************** print structure for direct access to objects ******************/
255 static void printfields(classinfo *c)
262 addoutputsize(sizeof(java_objectheader));
263 fprintf(file, " java_objectheader header;\n");
267 printfields(c->super);
269 for (i = 0; i < c->fieldscount; i++) {
272 if (!(f->flags & ACC_STATIC)) {
274 printtype(f->descriptor->text);
276 utf_fprint(file, f->name);
278 /* rename multiple fieldnames */
279 if ((ident_count = searchidentchain_utf(f->name)))
280 fprintf(file, "%d", ident_count - 1);
281 chain_addlast(ident_chain, f->name);
283 fprintf(file, ";\n");
289 /***************** store prototype for native method in file ******************/
291 static void printmethod(methodinfo *m)
297 /* search for return-type in descriptor */
298 utf_ptr = m->descriptor->text;
299 while (utf_nextu2(&utf_ptr) != ')');
302 fprintf(file, "\n/*\n * Class: ");
303 utf_fprint(file, m->class->name);
304 fprintf(file, "\n * Method: ");
305 utf_fprint(file, m->name);
306 fprintf(file, "\n * Signature: ");
307 utf_fprint(file, m->descriptor);
308 fprintf(file, "\n */\n");
310 /* create prototype */
311 fprintf(file, "JNIEXPORT ");
313 fprintf(file, " JNICALL Java_");
314 printID(m->class->name);
316 /* rename overloaded method */
317 if ((ident_count = searchidentchain_utf(m->name)))
318 fprintf(file, "%d", ident_count - 1);
319 chain_addlast(ident_chain, m->name);
323 fprintf(file, "(JNIEnv *env");
325 utf_ptr = m->descriptor->text + 1;
327 if (!(m->flags & ACC_STATIC)) {
328 fprintf(file, ", struct ");
329 printID(m->class->name);
330 fprintf(file, "* this");
333 fprintf(file, ", jclass clazz");
336 if ((*utf_ptr) != ')') fprintf(file, ", ");
338 while ((*utf_ptr) != ')') {
339 utf_ptr = printtype(utf_ptr);
340 fprintf(file, " par%d", paramnum++);
341 if ((*utf_ptr)!=')') fprintf(file, ", ");
344 fprintf(file, ");\n\n");
348 /****************** remove package-name in fully-qualified classname *********************/
350 static void gen_header_filename(char *buffer, utf *u)
354 for (i = 0;i < utf_strlen(u); i++) {
355 if ((u->text[i] == '/') || (u->text[i] == '$')) buffer[i] = '_'; /* convert '$' and '/' to '_' */
356 else buffer[i] = u->text[i];
358 buffer[utf_strlen(u)] = '\0';
362 /*********** create headerfile for classes and store native methods in chain ************/
364 static void headerfile_generate(classinfo *c)
366 char header_filename[1024] = "";
367 char classname[1024];
368 char uclassname[1024];
372 /* store class in chain */
373 chain_addlast(nativeclass_chain, c);
375 /* open headerfile for class */
376 gen_header_filename(classname, c->name);
378 /* create chain for renaming fields */
379 ident_chain = chain_new();
381 sprintf(header_filename, "nat/%s.h", classname);
382 file = fopen(header_filename, "w");
383 if (!file) panic("Can not open file to store header information");
385 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
387 /* convert to uppercase */
388 for (i = 0; classname[i]; i++) {
389 uclassname[i] = toupper(classname[i]);
391 uclassname[i] = '\0';
393 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
395 /* create structure for direct access to objects */
396 fprintf(file, "/* Structure information for class: ");
397 utf_fprint(file, c->name);
398 fprintf(file, " */\n\n");
399 fprintf(file, "typedef struct ");
401 fprintf(file, " {\n");
407 fprintf(file, ";\n\n");
409 /* create chain for renaming overloaded methods */
410 chain_free(ident_chain);
411 ident_chain = chain_new();
413 /* create method-prototypes */
415 for (i = 0; i < c->methodscount; i++) {
417 m = &(c->methods[i]);
419 if (m->flags & ACC_NATIVE) {
420 chain_addlast(nativemethod_chain, m);
425 chain_free(ident_chain);
427 fprintf(file, "#endif\n\n");
432 /******** print classname, '$' used to seperate inner-class name ***********/
434 void print_classname (classinfo *clazz)
436 utf *u = clazz->name;
437 char *endpos = u->text + u->blength;
438 char *utf_ptr = u->text;
441 while (utf_ptr<endpos) {
442 if ((c=utf_nextu2(&utf_ptr)) == '_')
450 /*************** create table for locating native functions ****************/
452 static void printnativetableentry(methodinfo *m)
454 fprintf(file, " { \"");
455 print_classname(m->class);
456 fprintf(file, "\",\n \"");
457 utf_fprint(file, m->name);
458 fprintf(file, "\",\n \"");
459 utf_fprint(file, m->descriptor);
460 fprintf(file, "\",\n ");
462 if ((m->flags & ACC_STATIC) != 0)
463 fprintf(file, "true");
465 fprintf(file, "false");
467 fprintf(file, ",\n ");
468 fprintf(file, "(functionptr) Java_");
469 printID(m->class->name);
472 fprintf(file,"\n },\n");
476 /***************************************************************************
478 create the nativetypes-headerfile which includes
479 the headerfiles of the classes stored in the classes-chain
481 ****************************************************************************/
483 static void headers_finish()
487 char classname[1024];
489 file = fopen("nativetypes.hh", "w");
492 panic("Can not open file 'native.h' to store header information");
494 fprintf(file, "/* Headerfile for native methods: nativetypes.hh */\n");
495 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
496 fprintf(file, "\n/* include native-Headerfiles */\n\n");
498 c = chain_first(nativeclass_chain);
501 gen_header_filename(classname, c->name);
502 fprintf(file, "#include \"nat/%s.h\"\n", classname);
503 c = chain_next(nativeclass_chain);
507 chain_free(nativeclass_chain);
509 /* create table of native-methods */
511 file = fopen("nativetable.hh", "w");
514 panic("Can not open file 'nativetable' to store native-link-table");
516 fprintf(file, "/* Table of native methods: nativetables.hh */\n");
517 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
519 while ((m = chain_first(nativemethod_chain)) != NULL) {
520 chain_remove(nativemethod_chain);
521 printnativetableentry(m);
524 chain_free(nativemethod_chain);
529 /******************** internal function: print_usage ************************
531 Prints usage information for the JAVA header generator to stdout.
533 ***************************************************************************/
535 static void print_usage()
537 printf("Usage: cacaoh class [class..]\n");
542 /************************** Function: main *******************************
546 **************************************************************************/
548 int main(int argc, char **argv)
556 /********** internal (only used by main) *****************************/
558 char classpath[500] = "";
559 char offsets_filename[1024] = ""; /* phil */
560 u4 heapsize = 100000;
562 /*********** options so only headers are generated *******************/
564 makeinitializations = false;
567 /************ Collect some info from the environment *****************/
569 cp = getenv("CLASSPATH");
571 strcpy(classpath + strlen(classpath), ":");
572 strcpy(classpath + strlen(classpath), cp);
581 /**************************** Program start **************************/
584 log_text("Java - header-generator started");
586 sprintf(offsets_filename, "jit/%s/offsets.h", ARCH_DIR); /* phil */
587 file = fopen(offsets_filename, "w");
589 fprintf(stderr, "Can not open file '%s' for write", offsets_filename);
593 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
595 fprintf(file, "#define offobjvftbl %3d\n", (int) OFFSET(java_objectheader, vftbl));
596 fprintf(file, "#define offarraysize %3d\n", (int) OFFSET(java_arrayheader, size));
597 fprintf(file, "#define offobjarrdata %3d\n\n", (int) OFFSET(java_objectarray, data[0]));
598 fprintf(file, "#define offbaseval %3d\n", (int) OFFSET(vftbl, baseval));
599 fprintf(file, "#define offdiffval %3d\n\n", (int) OFFSET(vftbl, diffval));
601 fprintf(file, "#define offclassvftbl %3d\n", (int) OFFSET(classinfo, vftbl));
603 fprintf(file, "#define offjniitemtype %3d\n", (int) OFFSET(jni_callblock, itemtype));
604 fprintf(file, "#define offjniitem %3d\n", (int) OFFSET(jni_callblock, item));
605 fprintf(file, "#define sizejniblock %3d\n\n", (int) sizeof(jni_callblock));
610 suck_init(classpath);
613 heap_init(heapsize, heapsize, &dummy);
617 /*********************** Load JAVA classes **************************/
619 nativemethod_chain = chain_new();
620 nativeclass_chain = chain_new();
622 for (a = 1; a < argc; a++) {
625 /* convert classname */
626 for (i = strlen(cp) - 1; i >= 0; i--) {
634 topclass = loader_load(utf_new_char(cp));
636 headerfile_generate(topclass);
641 /************************ Release all resources **********************/
645 tables_close(literalstring_free);
648 /* Print "finished" message */
650 log_text("Java - header-generator stopped");
658 void setVMClassField(classinfo *c)
663 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this) {
669 * These are local overrides for various environment variables in Emacs.
670 * Please do not remove this and leave it at the end of the file, where
671 * Emacs will automagically detect them.
672 * ---------------------------------------------------------------------
675 * indent-tabs-mode: t