1 /* src/cacaoh/headers.c - functions for header generation
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
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
33 $Id: headers.c 4150 2006-01-12 21:11:00Z twisti $
47 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
48 # if defined(__DARWIN__)
51 # include <ucontext.h>
55 #include "mm/memory.h"
56 #include "native/include/java_lang_String.h"
57 #include "native/include/java_lang_Throwable.h"
58 #include "toolbox/chain.h"
59 #include "toolbox/logging.h"
60 #include "vm/builtin.h"
62 #include "vm/global.h"
63 #include "vm/method.h"
64 #include "vm/loader.h"
65 #include "vm/options.h"
66 #include "vm/stringlocal.h"
67 #include "vm/jit/asmpart.h"
70 #if defined(ENABLE_INTRP)
71 /* dummy interpreter stack to keep the compiler happy */
73 u1 intrp_main_stack[1];
77 /* for raising exceptions from native methods *********************************/
79 #if !defined(USE_THREADS) || !defined(NATIVE_THREADS)
80 java_objectheader *_no_threads_exceptionptr = NULL;
84 /* replace some non-vmcore functions ******************************************/
86 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
89 /* return something different than NULL, otherwise we get an exception */
91 return (functionptr) 1;
94 java_objectheader *native_new_and_init(classinfo *c) { return NULL; }
95 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s) { return NULL; }
96 java_objectheader *native_new_and_init_int(classinfo *c, s4 i) { return NULL; }
97 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t) { return NULL; }
100 #if defined(ENABLE_JIT)
101 java_objectheader *asm_calljavafunction(methodinfo *m,
102 void *arg1, void *arg2,
103 void *arg3, void *arg4)
107 #if defined(ENABLE_INTRP)
108 java_objectheader *intrp_asm_calljavafunction(methodinfo *m,
109 void *arg1, void *arg2,
110 void *arg3, void *arg4)
115 /* code patching functions */
116 void patcher_builtin_arraycheckcast(u1 *sp) {}
118 #if defined(__MIPS__)
119 long compare_and_swap(long *p, long oldval, long newval)
130 u1 *createcompilerstub(methodinfo *m) { return NULL; }
131 #if defined(ENABLE_INTRP)
132 u1 *intrp_createcompilerstub(methodinfo *m) { return NULL; }
135 u1 *codegen_createnativestub(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, u1 **stackTop) {}
141 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
143 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
145 void asm_handle_builtin_exception(classinfo *c) {}
148 #if defined(ENABLE_JIT)
149 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
152 #if defined(ENABLE_INTRP)
153 void intrp_asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
157 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
162 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
164 return typecheck_TRUE;
167 void typeinfo_init_classinfo(typeinfo *info,classinfo *c)
171 bool typeinfo_init_class(typeinfo *info,classref_or_classinfo c)
176 void typeinfo_print(FILE *file,typeinfo *info,int indent) {}
178 void exceptions_print_exception(java_objectheader *xptr) {}
179 void stacktrace_print_trace(java_objectheader *xptr) {}
182 /* exception functions ********************************************************/
184 /* these should not be called */
186 void throw_main_exception_exit(void) { assert(0); }
187 void throw_exception(void) { assert(0); }
188 void throw_exception_exit(void) { assert(0); }
190 java_objectheader *new_verifyerror(methodinfo *m, const char *message)
194 /* keep compiler happy */
199 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
203 /* keep compiler happy */
209 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
213 fprintf(stderr, "%s: ", exception);
215 va_start(ap, message);
216 vfprintf(stderr, message, ap);
225 void exceptions_throw_outofmemory_exit(void)
227 fprintf(stderr, "java.lang.InternalError: Out of memory\n");
232 java_objectheader *new_exception(const char *classname)
234 fprintf(stderr, "%s\n", classname);
237 /* keep compiler happy */
243 java_objectheader *new_exception_message(const char *classname, const char *message)
245 fprintf(stderr, "%s: %s\n", classname, message);
248 /* keep compiler happy */
254 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
256 fprintf(stderr, "%s: ", classname);
257 utf_display(message);
262 /* keep compiler happy */
268 java_objectheader *new_exception_javastring(const char *classname,
269 java_lang_String *message)
271 fprintf(stderr, "%s: ", classname);
272 /* TODO print message */
277 /* keep compiler happy */
283 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
287 utf_display(c->name);
288 fprintf(stderr, ": ");
290 va_start(ap, message);
291 vfprintf(stderr, message, ap);
298 /* keep compiler happy */
304 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
308 va_start(ap, message);
309 (void) new_classformaterror(c, message, ap);
314 java_objectheader *new_classnotfoundexception(utf *name)
316 fprintf(stderr, "java.lang.ClassNotFoundException: ");
317 utf_fprint(stderr, name);
322 /* keep compiler happy */
328 java_objectheader *new_noclassdeffounderror(utf *name)
330 fprintf(stderr, "java.lang.NoClassDefFoundError: ");
331 utf_fprint(stderr, name);
336 /* keep compiler happy */
342 java_objectheader *exceptions_new_linkageerror(const char *message,
345 fprintf(stderr, "java.lang.LinkageError: %s",message);
347 utf_fprint_classname(stderr, c->name);
353 /* keep compiler happy */
358 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
359 utf *name, utf *desc)
361 fprintf(stderr, "java.lang.NoSuchMethodError: ");
362 utf_fprint(stderr, c->name);
363 fprintf(stderr, ".");
364 utf_fprint(stderr, name);
365 utf_fprint(stderr, desc);
370 /* keep compiler happy */
376 java_objectheader *new_internalerror(const char *message, ...)
380 fprintf(stderr, "%s: ", string_java_lang_InternalError);
382 va_start(ap, message);
383 vfprintf(stderr, message, ap);
388 /* keep compiler happy */
394 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
398 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
400 utf_display(c->name);
401 fprintf(stderr, ": ");
403 va_start(ap, message);
404 vfprintf(stderr, message, ap);
409 /* keep compiler happy */
415 java_objectheader *new_illegalmonitorstateexception(void)
417 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
420 /* keep compiler happy */
426 java_objectheader *new_negativearraysizeexception(void)
428 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
431 /* keep compiler happy */
437 void exceptions_throw_negativearraysizeexception(void)
439 (void) new_negativearraysizeexception();
443 java_objectheader *new_nullpointerexception(void)
445 fprintf(stderr, "%s", string_java_lang_NullPointerException);
448 /* keep compiler happy */
454 void exceptions_throw_nullpointerexception(void)
456 (void) new_nullpointerexception();
460 void classnotfoundexception_to_noclassdeffounderror(void)
464 /* machine dependent stuff ****************************************************/
466 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
467 threadcritnode asm_criticalsections;
468 void thread_restartcriticalsection(ucontext_t *uc) {}
471 void md_param_alloc(methoddesc *md) {}
474 #if defined(ENABLE_INTRP)
475 void print_dynamic_super_statistics(void) {}
479 /************************ global variables **********************/
481 chain *nativemethod_chain; /* chain with native methods */
482 chain *nativeclass_chain; /* chain with processed classes */
483 chain *ident_chain; /* chain with method and field names in current class */
485 static u4 outputsize;
486 static bool dopadding;
489 static void printIDpart(int c)
491 if ((c >= 'a' && c <= 'z') ||
492 (c >= 'A' && c <= 'Z') ||
493 (c >= '0' && c <= '9') ||
503 char *utf_ptr = u->text;
506 for (i = 0; i < utf_strlen(u); i++)
507 printIDpart(utf_nextu2(&utf_ptr));
511 static void addoutputsize (int len)
514 if (!dopadding) return;
516 newsize = ALIGN(outputsize, len);
518 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
519 outputsize = newsize;
523 void printOverloadPart(utf *desc)
525 char *utf_ptr=desc->text;
530 while ((c = utf_nextu2(&utf_ptr)) != ')') {
540 fprintf(file, "%c", (char) c);
547 while ((c = utf_nextu2(&utf_ptr)) != ';')
554 log_text("invalid method descriptor");
560 static char *printtype(char *utf_ptr)
564 switch (utf_nextu2(&utf_ptr)) {
565 case 'V': fprintf (file, "void");
571 case 'Z': addoutputsize (4);
572 fprintf (file, "s4");
574 case 'J': addoutputsize (8);
575 fprintf (file, "s8");
577 case 'F': addoutputsize (4);
578 fprintf (file, "float");
580 case 'D': addoutputsize (8);
581 fprintf (file, "double");
584 addoutputsize ( sizeof(java_arrayheader*) );
585 switch (utf_nextu2(&utf_ptr)) {
586 case 'I': fprintf (file, "java_intarray*"); break;
587 case 'J': fprintf (file, "java_longarray*"); break;
588 case 'Z': fprintf (file, "java_booleanarray*"); break;
589 case 'B': fprintf (file, "java_bytearray*"); break;
590 case 'S': fprintf (file, "java_shortarray*"); break;
591 case 'C': fprintf (file, "java_chararray*"); break;
592 case 'F': fprintf (file, "java_floatarray*"); break;
593 case 'D': fprintf (file, "java_doublearray*"); break;
595 case '[': fprintf(file, "java_objectarray*");
596 while ((c = utf_nextu2(&utf_ptr)) == '[');
598 while (utf_nextu2(&utf_ptr) != ';');
601 case 'L': fprintf(file, "java_objectarray*");
602 while (utf_nextu2(&utf_ptr) != ';');
605 log_text("invalid type descriptor");
611 addoutputsize ( sizeof(java_objectheader*));
612 fprintf (file, "struct ");
613 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
618 log_text("Unknown type in field descriptor");
626 /***** determine the number of entries of a utf string in the ident chain *****/
628 static int searchidentchain_utf(utf *ident)
630 utf *u = chain_first(ident_chain); /* first element of list */
634 if (u==ident) count++; /* string found */
635 u = chain_next(ident_chain); /* next element in list */
642 /************** print structure for direct access to objects ******************/
644 static void printfields(classinfo *c)
651 addoutputsize(sizeof(java_objectheader));
652 fprintf(file, " java_objectheader header;\n");
656 printfields(c->super.cls);
658 for (i = 0; i < c->fieldscount; i++) {
661 if (!(f->flags & ACC_STATIC)) {
663 printtype(f->descriptor->text);
665 utf_fprint(file, f->name);
667 /* rename multiple fieldnames */
668 if ((ident_count = searchidentchain_utf(f->name)))
669 fprintf(file, "%d", ident_count - 1);
670 chain_addlast(ident_chain, f->name);
672 fprintf(file, ";\n");
678 /***************** store prototype for native method in file ******************/
680 void printmethod(methodinfo *m)
685 /* search for return-type in descriptor */
686 utf_ptr = m->descriptor->text;
687 while (utf_nextu2(&utf_ptr) != ')');
690 fprintf(file, "\n/*\n * Class: ");
691 utf_fprint(file, m->class->name);
692 fprintf(file, "\n * Method: ");
693 utf_fprint(file, m->name);
694 fprintf(file, "\n * Signature: ");
695 utf_fprint(file, m->descriptor);
696 fprintf(file, "\n */\n");
698 /* create prototype */
699 fprintf(file, "JNIEXPORT ");
701 fprintf(file, " JNICALL Java_");
702 printID(m->class->name);
704 chain_addlast(ident_chain, m->name);
708 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
709 fprintf(file, "(JNIEnv *env");
711 utf_ptr = m->descriptor->text + 1;
713 if (!(m->flags & ACC_STATIC)) {
714 fprintf(file, ", struct ");
715 printID(m->class->name);
716 fprintf(file, "* this");
719 fprintf(file, ", jclass clazz");
722 if ((*utf_ptr) != ')') fprintf(file, ", ");
724 while ((*utf_ptr) != ')') {
725 utf_ptr = printtype(utf_ptr);
726 fprintf(file, " par%d", paramnum++);
727 if ((*utf_ptr)!=')') fprintf(file, ", ");
730 fprintf(file, ");\n\n");
734 /******* remove package-name in fully-qualified classname *********************/
736 void gen_header_filename(char *buffer, utf *u)
740 for (i = 0; i < utf_strlen(u); i++) {
741 if ((u->text[i] == '/') || (u->text[i] == '$')) {
742 buffer[i] = '_'; /* convert '$' and '/' to '_' */
745 buffer[i] = u->text[i];
748 buffer[utf_strlen(u)] = '\0';
752 /* create headerfile for classes and store native methods in chain ************/
754 void headerfile_generate(classinfo *c, char *opt_directory)
756 char header_filename[1024] = "";
757 char classname[1024];
758 char uclassname[1024];
763 u2 nativelyoverloaded;
765 /* store class in chain */
766 chain_addlast(nativeclass_chain, c);
768 /* open headerfile for class */
769 gen_header_filename(classname, c->name);
771 /* create chain for renaming fields */
772 ident_chain = chain_new();
775 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
778 sprintf(header_filename, "%s.h", classname);
781 file = fopen(header_filename, "w");
783 log_text("Can not open file to store header information");
787 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
789 /* convert to uppercase */
790 for (i = 0; classname[i]; i++) {
791 uclassname[i] = toupper(classname[i]);
793 uclassname[i] = '\0';
795 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
797 /* create structure for direct access to objects */
798 fprintf(file, "/* Structure information for class: ");
799 utf_fprint(file, c->name);
800 fprintf(file, " */\n\n");
801 fprintf(file, "typedef struct ");
803 fprintf(file, " {\n");
811 fprintf(file, ";\n\n");
813 /* create chain for renaming overloaded methods */
814 chain_free(ident_chain);
815 ident_chain = chain_new();
817 /* create method-prototypes */
819 /* find overloaded methods */
820 for (i = 0; i < c->methodscount; i++) {
822 m = &(c->methods[i]);
824 if (!(m->flags & ACC_NATIVE)) continue;
825 if (!m->nativelyoverloaded) {
826 nativelyoverloaded=false;
827 for (i2=i+1;i2<c->methodscount; i2++) {
828 m2 = &(c->methods[i2]);
829 if (!(m2->flags & ACC_NATIVE)) continue;
830 if (m->name==m2->name) {
831 m2->nativelyoverloaded=true;
832 nativelyoverloaded=true;
835 m->nativelyoverloaded=nativelyoverloaded;
840 for (i = 0; i < c->methodscount; i++) {
842 m = &(c->methods[i]);
844 if (m->flags & ACC_NATIVE) {
845 chain_addlast(nativemethod_chain, m);
850 chain_free(ident_chain);
852 fprintf(file, "#endif\n\n");
858 /******** print classname, '$' used to seperate inner-class name ***********/
860 void print_classname(classinfo *clazz)
862 utf *u = clazz->name;
863 char *endpos = u->text + u->blength;
864 char *utf_ptr = u->text;
867 while (utf_ptr < endpos) {
868 if ((c = utf_nextu2(&utf_ptr)) == '_')
877 * These are local overrides for various environment variables in Emacs.
878 * Please do not remove this and leave it at the end of the file, where
879 * Emacs will automagically detect them.
880 * ---------------------------------------------------------------------
883 * indent-tabs-mode: t