1 /* src/cacaoh/headers.c - functions for header generation
3 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
27 Authors: Reinhard Grafl
33 $Id: headers.c 5989 2006-11-15 18:01:17Z twisti $
48 #if defined(ENABLE_THREADS)
49 # if defined(__DARWIN__)
52 # include <ucontext.h>
55 #include "mm/gc-common.h"
56 #include "mm/memory.h"
57 #include "native/include/java_lang_String.h"
58 #include "native/include/java_lang_Throwable.h"
59 #include "toolbox/chain.h"
60 #include "toolbox/logging.h"
61 #include "vm/builtin.h"
63 #include "vm/global.h"
64 #include "vm/method.h"
65 #include "vm/loader.h"
66 #include "vm/options.h"
67 #include "vm/stringlocal.h"
68 #include "vm/jit/asmpart.h"
71 /* Invocation API variables ***************************************************/
73 _Jv_JavaVM *_Jv_jvm; /* denotes a Java VM */
74 char *_Jv_bootclasspath;
77 char *classpath_libdir;
80 char *_Jv_java_library_path;
82 #if defined(ENABLE_INTRP)
83 /* dummy interpreter stack to keep the compiler happy */
91 bool inline_debug_log = false;
95 /* for raising exceptions from native methods *********************************/
97 #if !defined(ENABLE_THREADS)
98 java_objectheader *_no_threads_exceptionptr = NULL;
102 /* replace some non-vmcore functions ******************************************/
104 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
107 /* return something different than NULL, otherwise we get an exception */
109 return (functionptr) 1;
112 java_objectheader *native_new_and_init(classinfo *c) { return NULL; }
113 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s) { return NULL; }
114 java_objectheader *native_new_and_init_int(classinfo *c, s4 i) { return NULL; }
115 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t) { return NULL; }
118 java_objectheader *vm_call_method(methodinfo *m, java_objectheader *o, ...)
121 void vm_abort(const char *text, ...)
125 /* print the log message */
128 vfprintf(stderr, text, ap);
131 /* now abort the VM */
136 /* code patching functions */
137 void patcher_builtin_arraycheckcast(u1 *sp) {}
139 #if defined(__MIPS__)
140 long compare_and_swap(long *p, long oldval, long newval)
151 u1 *createcompilerstub(methodinfo *m) { return NULL; }
152 #if defined(ENABLE_INTRP)
153 u1 *intrp_createcompilerstub(methodinfo *m) { return NULL; }
156 codeinfo *codegen_createnativestub(functionptr f, methodinfo *m) { return NULL; }
158 void removecompilerstub(u1 *stub) {}
159 void removenativestub(u1 *stub) {}
161 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
162 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
164 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
166 void asm_handle_builtin_exception(classinfo *c) {}
168 #if defined(ENABLE_JIT)
169 void asm_abstractmethoderror(void) {}
170 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
173 #if defined(ENABLE_INTRP)
174 void intrp_asm_abstractmethoderror(void) {}
175 void intrp_asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
178 u8 asm_get_cycle_count(void)
184 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
189 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
191 return typecheck_TRUE;
194 void typeinfo_init_classinfo(typeinfo *info,classinfo *c)
198 bool typeinfo_init_class(typeinfo *info,classref_or_classinfo c)
203 void typeinfo_print(FILE *file,typeinfo *info,int indent) {}
204 void typeinfo_print_short(FILE *file,typeinfo *info) {}
206 void exceptions_print_exception(java_objectheader *xptr) {}
207 void stacktrace_dump_trace(threadobject *thread) {}
208 void stacktrace_print_trace(java_objectheader *xptr) {}
209 java_objectarray *stacktrace_getClassContext() { return NULL; }
210 void code_free_code_of_method(methodinfo *m) {}
212 void jit_invalidate_code(methodinfo *m) {}
215 /* exception functions ********************************************************/
217 /* these should not be called */
219 void throw_main_exception_exit(void) { assert(0); }
220 void throw_exception(void) { assert(0); }
221 void throw_exception_exit(void) { assert(0); }
223 void exceptions_throw_verifyerror(methodinfo *m, const char *message)
228 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
232 /* keep compiler happy */
238 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
242 fprintf(stderr, "%s: ", exception);
244 va_start(ap, message);
245 vfprintf(stderr, message, ap);
254 void exceptions_throw_outofmemory_exit(void)
256 fprintf(stderr, "java.lang.InternalError: Out of memory\n");
261 java_objectheader *new_exception(const char *classname)
263 fprintf(stderr, "%s\n", classname);
266 /* keep compiler happy */
272 java_objectheader *new_exception_message(const char *classname, const char *message)
274 fprintf(stderr, "%s: %s\n", classname, message);
277 /* keep compiler happy */
283 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
285 fprintf(stderr, "%s: ", classname);
286 utf_display_printable_ascii(message);
291 /* keep compiler happy */
297 java_objectheader *new_exception_javastring(const char *classname,
298 java_lang_String *message)
300 fprintf(stderr, "%s: ", classname);
301 /* TODO print message */
306 /* keep compiler happy */
312 void exceptions_throw_abstractmethoderror(void)
314 fprintf(stderr, "java.lang.AbstractMethodError\n");
320 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
324 utf_display_printable_ascii(c->name);
325 fprintf(stderr, ": ");
327 va_start(ap, message);
328 vfprintf(stderr, message, ap);
335 /* keep compiler happy */
341 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
345 va_start(ap, message);
346 (void) new_classformaterror(c, message, ap);
351 java_objectheader *new_classnotfoundexception(utf *name)
353 fprintf(stderr, "java.lang.ClassNotFoundException: ");
354 utf_fprint_printable_ascii(stderr, name);
359 /* keep compiler happy */
365 java_objectheader *new_noclassdeffounderror(utf *name)
367 fprintf(stderr, "java.lang.NoClassDefFoundError: ");
368 utf_fprint_printable_ascii(stderr, name);
373 /* keep compiler happy */
379 java_objectheader *exceptions_new_linkageerror(const char *message,
382 fprintf(stderr, "java.lang.LinkageError: %s",message);
384 utf_fprint_printable_ascii_classname(stderr, c->name);
390 /* keep compiler happy */
395 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
396 utf *name, utf *desc)
398 fprintf(stderr, "java.lang.NoSuchMethodError: ");
399 utf_fprint_printable_ascii(stderr, c->name);
400 fprintf(stderr, ".");
401 utf_fprint_printable_ascii(stderr, name);
402 utf_fprint_printable_ascii(stderr, desc);
407 /* keep compiler happy */
413 void exceptions_throw_nosuchmethoderror(classinfo *c, utf *name, utf *desc)
415 (void) exceptions_new_nosuchmethoderror(c, name, desc);
419 java_objectheader *new_internalerror(const char *message, ...)
423 fprintf(stderr, "%s: ", string_java_lang_InternalError);
425 va_start(ap, message);
426 vfprintf(stderr, message, ap);
431 /* keep compiler happy */
437 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
441 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
443 utf_display_printable_ascii(c->name);
444 fprintf(stderr, ": ");
446 va_start(ap, message);
447 vfprintf(stderr, message, ap);
452 /* keep compiler happy */
458 void exceptions_throw_arrayindexoutofboundsexception(void)
460 fprintf(stderr, "%s", string_java_lang_ArrayIndexOutOfBoundsException);
465 java_objectheader *exceptions_new_arraystoreexception(void)
467 fprintf(stderr, "%s", string_java_lang_ArrayStoreException);
470 /* keep compiler happy */
476 void exceptions_throw_arraystoreexception(void)
478 (void) exceptions_new_arraystoreexception();
482 java_objectheader *exceptions_throw_illegalmonitorstateexception(void)
484 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
487 /* keep compiler happy */
493 java_objectheader *new_negativearraysizeexception(void)
495 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
498 /* keep compiler happy */
504 void exceptions_throw_negativearraysizeexception(void)
506 (void) new_negativearraysizeexception();
510 java_objectheader *exceptions_new_nullpointerexception(void)
512 fprintf(stderr, "%s", string_java_lang_NullPointerException);
515 /* keep compiler happy */
521 void exceptions_throw_nullpointerexception(void)
523 (void) exceptions_new_nullpointerexception();
527 void classnotfoundexception_to_noclassdeffounderror(void)
531 /* machine dependent stuff ****************************************************/
533 #if defined(ENABLE_THREADS)
534 critical_section_node_t asm_criticalsections;
535 void thread_restartcriticalsection(ucontext_t *uc) {}
538 void md_param_alloc(methoddesc *md) {}
541 #if defined(ENABLE_INTRP)
542 void print_dynamic_super_statistics(void) {}
546 /************************ global variables **********************/
548 #define ACC_NATIVELY_OVERLOADED 0x10000000
550 chain *ident_chain; /* chain with method and field names in current class */
552 static u4 outputsize;
553 static bool dopadding;
556 static void printIDpart(int c)
558 if ((c >= 'a' && c <= 'z') ||
559 (c >= 'A' && c <= 'Z') ||
560 (c >= '0' && c <= '9') ||
570 char *utf_ptr = u->text;
573 for (i = 0; i < utf_get_number_of_u2s(u); i++)
574 printIDpart(utf_nextu2(&utf_ptr));
578 static void addoutputsize (int len)
581 if (!dopadding) return;
583 newsize = MEMORY_ALIGN(outputsize, len);
585 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
586 outputsize = newsize;
590 void printOverloadPart(utf *desc)
592 char *utf_ptr=desc->text;
597 while ((c = utf_nextu2(&utf_ptr)) != ')') {
607 fprintf(file, "%c", (char) c);
614 while ((c = utf_nextu2(&utf_ptr)) != ';')
621 log_text("invalid method descriptor");
627 static char *printtype(char *utf_ptr)
631 switch (utf_nextu2(&utf_ptr)) {
632 case 'V': fprintf (file, "void");
638 case 'Z': addoutputsize (4);
639 fprintf (file, "s4");
641 case 'J': addoutputsize (8);
642 fprintf (file, "s8");
644 case 'F': addoutputsize (4);
645 fprintf (file, "float");
647 case 'D': addoutputsize (8);
648 fprintf (file, "double");
651 addoutputsize ( sizeof(java_arrayheader*) );
652 switch (utf_nextu2(&utf_ptr)) {
653 case 'I': fprintf (file, "java_intarray*"); break;
654 case 'J': fprintf (file, "java_longarray*"); break;
655 case 'Z': fprintf (file, "java_booleanarray*"); break;
656 case 'B': fprintf (file, "java_bytearray*"); break;
657 case 'S': fprintf (file, "java_shortarray*"); break;
658 case 'C': fprintf (file, "java_chararray*"); break;
659 case 'F': fprintf (file, "java_floatarray*"); break;
660 case 'D': fprintf (file, "java_doublearray*"); break;
662 case '[': fprintf(file, "java_objectarray*");
663 while ((c = utf_nextu2(&utf_ptr)) == '[');
665 while (utf_nextu2(&utf_ptr) != ';');
668 case 'L': fprintf(file, "java_objectarray*");
669 while (utf_nextu2(&utf_ptr) != ';');
672 log_text("invalid type descriptor");
678 addoutputsize ( sizeof(java_objectheader*));
679 fprintf (file, "struct ");
680 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
685 log_text("Unknown type in field descriptor");
693 /***** determine the number of entries of a utf string in the ident chain *****/
695 static int searchidentchain_utf(utf *ident)
697 utf *u = chain_first(ident_chain); /* first element of list */
701 if (u==ident) count++; /* string found */
702 u = chain_next(ident_chain); /* next element in list */
709 /************** print structure for direct access to objects ******************/
711 static void printfields(classinfo *c)
718 addoutputsize(sizeof(java_objectheader));
719 fprintf(file, " java_objectheader header;\n");
723 printfields(c->super.cls);
725 for (i = 0; i < c->fieldscount; i++) {
728 if (!(f->flags & ACC_STATIC)) {
730 printtype(f->descriptor->text);
732 utf_fprint_printable_ascii(file, f->name);
734 /* rename multiple fieldnames */
735 if ((ident_count = searchidentchain_utf(f->name)))
736 fprintf(file, "%d", ident_count - 1);
737 chain_addlast(ident_chain, f->name);
739 fprintf(file, ";\n");
745 /***************** store prototype for native method in file ******************/
747 void printmethod(methodinfo *m)
752 /* search for return-type in descriptor */
753 utf_ptr = m->descriptor->text;
754 while (utf_nextu2(&utf_ptr) != ')');
757 fprintf(file, "\n/*\n * Class: ");
758 utf_fprint_printable_ascii(file, m->class->name);
759 fprintf(file, "\n * Method: ");
760 utf_fprint_printable_ascii(file, m->name);
761 fprintf(file, "\n * Signature: ");
762 utf_fprint_printable_ascii(file, m->descriptor);
763 fprintf(file, "\n */\n");
765 /* create prototype */
766 fprintf(file, "JNIEXPORT ");
768 fprintf(file, " JNICALL Java_");
769 printID(m->class->name);
771 chain_addlast(ident_chain, m->name);
776 /* ATTENTION: We use a dummy flag here. */
778 if (m->flags & ACC_NATIVELY_OVERLOADED)
779 printOverloadPart(m->descriptor);
781 fprintf(file, "(JNIEnv *env");
783 utf_ptr = m->descriptor->text + 1;
785 if (!(m->flags & ACC_STATIC)) {
786 fprintf(file, ", struct ");
787 printID(m->class->name);
788 fprintf(file, "* this");
791 fprintf(file, ", jclass clazz");
794 if ((*utf_ptr) != ')') fprintf(file, ", ");
796 while ((*utf_ptr) != ')') {
797 utf_ptr = printtype(utf_ptr);
798 fprintf(file, " par%d", paramnum++);
799 if ((*utf_ptr)!=')') fprintf(file, ", ");
802 fprintf(file, ");\n\n");
806 /******* remove package-name in fully-qualified classname *********************/
808 void gen_header_filename(char *buffer, utf *u)
812 for (i = 0; i < utf_get_number_of_u2s(u); i++) {
813 if ((u->text[i] == '/') || (u->text[i] == '$')) {
814 buffer[i] = '_'; /* convert '$' and '/' to '_' */
817 buffer[i] = u->text[i];
820 buffer[utf_get_number_of_u2s(u)] = '\0';
824 /* create headerfile for classes and store native methods in chain ************/
826 void headerfile_generate(classinfo *c, char *opt_directory)
828 char header_filename[1024] = "";
829 char classname[1024];
830 char uclassname[1024];
835 bool nativelyoverloaded;
837 /* prevent compiler warnings */
839 nativelyoverloaded = false;
841 /* open headerfile for class */
842 gen_header_filename(classname, c->name);
844 /* create chain for renaming fields */
845 ident_chain = chain_new();
848 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
851 sprintf(header_filename, "%s.h", classname);
854 file = fopen(header_filename, "w");
856 log_text("Can not open file to store header information");
860 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
862 /* convert to uppercase */
863 for (i = 0; classname[i]; i++) {
864 uclassname[i] = toupper(classname[i]);
866 uclassname[i] = '\0';
868 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
870 /* create structure for direct access to objects */
871 fprintf(file, "/* Structure information for class: ");
872 utf_fprint_printable_ascii(file, c->name);
873 fprintf(file, " */\n\n");
874 fprintf(file, "typedef struct ");
876 fprintf(file, " {\n");
884 fprintf(file, ";\n\n");
886 /* create chain for renaming overloaded methods */
887 chain_free(ident_chain);
888 ident_chain = chain_new();
890 /* create method-prototypes */
892 /* find overloaded methods */
894 for (i = 0; i < c->methodscount; i++) {
895 m = &(c->methods[i]);
897 if (!(m->flags & ACC_NATIVE))
900 /* We use a dummy flag here. */
902 if (!(m->flags & ACC_NATIVELY_OVERLOADED)) {
903 nativelyoverloaded = false;
905 for (j = i + 1; j < c->methodscount; j++) {
906 m2 = &(c->methods[j]);
908 if (!(m2->flags & ACC_NATIVE))
911 if (m->name == m2->name) {
912 m2->flags |= ACC_NATIVELY_OVERLOADED;
913 nativelyoverloaded = true;
918 if (nativelyoverloaded == true)
919 m->flags |= ACC_NATIVELY_OVERLOADED;
922 for (i = 0; i < c->methodscount; i++) {
923 m = &(c->methods[i]);
925 if (m->flags & ACC_NATIVE)
929 chain_free(ident_chain);
931 fprintf(file, "#endif\n\n");
937 /******** print classname, '$' used to seperate inner-class name ***********/
939 void print_classname(classinfo *clazz)
941 utf *u = clazz->name;
942 char *endpos = u->text + u->blength;
943 char *utf_ptr = u->text;
946 while (utf_ptr < endpos) {
947 if ((c = utf_nextu2(&utf_ptr)) == '_')
954 /* jvmti releated functions ************************************************/
956 #if defined(ENABLE_JVMTI)
957 void jvmti_ThreadStartEnd(int ev) {;}
958 void jvmti_ClassLoadPrepare(bool prepared, classinfo *c) {;}
959 void jvmti_MonitorContendedEntering(bool entered, jobject obj) {;}
965 * These are local overrides for various environment variables in Emacs.
966 * Please do not remove this and leave it at the end of the file, where
967 * Emacs will automagically detect them.
968 * ---------------------------------------------------------------------
971 * indent-tabs-mode: t
975 * vim:noexpandtab:sw=4:ts=4: