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 6244 2006-12-27 15:15:31Z 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_virtualmachineerror(void)
460 fprintf(stderr, "%s", string_java_lang_VirtualMachineError);
465 void exceptions_throw_arrayindexoutofboundsexception(void)
467 fprintf(stderr, "%s", string_java_lang_ArrayIndexOutOfBoundsException);
472 java_objectheader *exceptions_new_arraystoreexception(void)
474 fprintf(stderr, "%s", string_java_lang_ArrayStoreException);
477 /* keep compiler happy */
483 void exceptions_throw_arraystoreexception(void)
485 (void) exceptions_new_arraystoreexception();
489 java_objectheader *exceptions_throw_illegalmonitorstateexception(void)
491 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
494 /* keep compiler happy */
500 java_objectheader *new_negativearraysizeexception(void)
502 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
505 /* keep compiler happy */
511 void exceptions_throw_negativearraysizeexception(void)
513 (void) new_negativearraysizeexception();
517 java_objectheader *exceptions_new_nullpointerexception(void)
519 fprintf(stderr, "%s", string_java_lang_NullPointerException);
522 /* keep compiler happy */
528 void exceptions_throw_nullpointerexception(void)
530 (void) exceptions_new_nullpointerexception();
534 void classnotfoundexception_to_noclassdeffounderror(void)
538 /* machine dependent stuff ****************************************************/
540 #if defined(ENABLE_THREADS)
541 critical_section_node_t asm_criticalsections;
542 void thread_restartcriticalsection(ucontext_t *uc) {}
545 void md_param_alloc(methoddesc *md) {}
548 #if defined(ENABLE_INTRP)
549 void print_dynamic_super_statistics(void) {}
553 /************************ global variables **********************/
555 #define ACC_NATIVELY_OVERLOADED 0x10000000
557 chain *ident_chain; /* chain with method and field names in current class */
559 static u4 outputsize;
560 static bool dopadding;
563 static void printIDpart(int c)
565 if ((c >= 'a' && c <= 'z') ||
566 (c >= 'A' && c <= 'Z') ||
567 (c >= '0' && c <= '9') ||
577 char *utf_ptr = u->text;
580 for (i = 0; i < utf_get_number_of_u2s(u); i++)
581 printIDpart(utf_nextu2(&utf_ptr));
585 static void addoutputsize (int len)
588 if (!dopadding) return;
590 newsize = MEMORY_ALIGN(outputsize, len);
592 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
593 outputsize = newsize;
597 void printOverloadPart(utf *desc)
599 char *utf_ptr=desc->text;
604 while ((c = utf_nextu2(&utf_ptr)) != ')') {
614 fprintf(file, "%c", (char) c);
621 while ((c = utf_nextu2(&utf_ptr)) != ';')
628 log_text("invalid method descriptor");
634 static char *printtype(char *utf_ptr)
638 switch (utf_nextu2(&utf_ptr)) {
639 case 'V': fprintf (file, "void");
645 case 'Z': addoutputsize (4);
646 fprintf (file, "s4");
648 case 'J': addoutputsize (8);
649 fprintf (file, "s8");
651 case 'F': addoutputsize (4);
652 fprintf (file, "float");
654 case 'D': addoutputsize (8);
655 fprintf (file, "double");
658 addoutputsize ( sizeof(java_arrayheader*) );
659 switch (utf_nextu2(&utf_ptr)) {
660 case 'I': fprintf (file, "java_intarray*"); break;
661 case 'J': fprintf (file, "java_longarray*"); break;
662 case 'Z': fprintf (file, "java_booleanarray*"); break;
663 case 'B': fprintf (file, "java_bytearray*"); break;
664 case 'S': fprintf (file, "java_shortarray*"); break;
665 case 'C': fprintf (file, "java_chararray*"); break;
666 case 'F': fprintf (file, "java_floatarray*"); break;
667 case 'D': fprintf (file, "java_doublearray*"); break;
669 case '[': fprintf(file, "java_objectarray*");
670 while ((c = utf_nextu2(&utf_ptr)) == '[');
672 while (utf_nextu2(&utf_ptr) != ';');
675 case 'L': fprintf(file, "java_objectarray*");
676 while (utf_nextu2(&utf_ptr) != ';');
679 log_text("invalid type descriptor");
685 addoutputsize ( sizeof(java_objectheader*));
686 fprintf (file, "struct ");
687 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
692 log_text("Unknown type in field descriptor");
700 /***** determine the number of entries of a utf string in the ident chain *****/
702 static int searchidentchain_utf(utf *ident)
704 utf *u = chain_first(ident_chain); /* first element of list */
708 if (u==ident) count++; /* string found */
709 u = chain_next(ident_chain); /* next element in list */
716 /************** print structure for direct access to objects ******************/
718 static void printfields(classinfo *c)
725 addoutputsize(sizeof(java_objectheader));
726 fprintf(file, " java_objectheader header;\n");
730 printfields(c->super.cls);
732 for (i = 0; i < c->fieldscount; i++) {
735 if (!(f->flags & ACC_STATIC)) {
737 printtype(f->descriptor->text);
739 utf_fprint_printable_ascii(file, f->name);
741 /* rename multiple fieldnames */
742 if ((ident_count = searchidentchain_utf(f->name)))
743 fprintf(file, "%d", ident_count - 1);
744 chain_addlast(ident_chain, f->name);
746 fprintf(file, ";\n");
752 /***************** store prototype for native method in file ******************/
754 void printmethod(methodinfo *m)
759 /* search for return-type in descriptor */
760 utf_ptr = m->descriptor->text;
761 while (utf_nextu2(&utf_ptr) != ')');
764 fprintf(file, "\n/*\n * Class: ");
765 utf_fprint_printable_ascii(file, m->class->name);
766 fprintf(file, "\n * Method: ");
767 utf_fprint_printable_ascii(file, m->name);
768 fprintf(file, "\n * Signature: ");
769 utf_fprint_printable_ascii(file, m->descriptor);
770 fprintf(file, "\n */\n");
772 /* create prototype */
773 fprintf(file, "JNIEXPORT ");
775 fprintf(file, " JNICALL Java_");
776 printID(m->class->name);
778 chain_addlast(ident_chain, m->name);
783 /* ATTENTION: We use a dummy flag here. */
785 if (m->flags & ACC_NATIVELY_OVERLOADED)
786 printOverloadPart(m->descriptor);
788 fprintf(file, "(JNIEnv *env");
790 utf_ptr = m->descriptor->text + 1;
792 if (!(m->flags & ACC_STATIC)) {
793 fprintf(file, ", struct ");
794 printID(m->class->name);
795 fprintf(file, "* this");
798 fprintf(file, ", jclass clazz");
801 if ((*utf_ptr) != ')') fprintf(file, ", ");
803 while ((*utf_ptr) != ')') {
804 utf_ptr = printtype(utf_ptr);
805 fprintf(file, " par%d", paramnum++);
806 if ((*utf_ptr)!=')') fprintf(file, ", ");
809 fprintf(file, ");\n\n");
813 /******* remove package-name in fully-qualified classname *********************/
815 void gen_header_filename(char *buffer, utf *u)
819 for (i = 0; i < utf_get_number_of_u2s(u); i++) {
820 if ((u->text[i] == '/') || (u->text[i] == '$')) {
821 buffer[i] = '_'; /* convert '$' and '/' to '_' */
824 buffer[i] = u->text[i];
827 buffer[utf_get_number_of_u2s(u)] = '\0';
831 /* create headerfile for classes and store native methods in chain ************/
833 void headerfile_generate(classinfo *c, char *opt_directory)
835 char header_filename[1024] = "";
836 char classname[1024];
837 char uclassname[1024];
842 bool nativelyoverloaded;
844 /* prevent compiler warnings */
846 nativelyoverloaded = false;
848 /* open headerfile for class */
849 gen_header_filename(classname, c->name);
851 /* create chain for renaming fields */
852 ident_chain = chain_new();
855 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
858 sprintf(header_filename, "%s.h", classname);
861 file = fopen(header_filename, "w");
863 log_text("Can not open file to store header information");
867 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
869 /* convert to uppercase */
870 for (i = 0; classname[i]; i++) {
871 uclassname[i] = toupper(classname[i]);
873 uclassname[i] = '\0';
875 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
877 /* create structure for direct access to objects */
878 fprintf(file, "/* Structure information for class: ");
879 utf_fprint_printable_ascii(file, c->name);
880 fprintf(file, " */\n\n");
881 fprintf(file, "typedef struct ");
883 fprintf(file, " {\n");
891 fprintf(file, ";\n\n");
893 /* create chain for renaming overloaded methods */
894 chain_free(ident_chain);
895 ident_chain = chain_new();
897 /* create method-prototypes */
899 /* find overloaded methods */
901 for (i = 0; i < c->methodscount; i++) {
902 m = &(c->methods[i]);
904 if (!(m->flags & ACC_NATIVE))
907 /* We use a dummy flag here. */
909 if (!(m->flags & ACC_NATIVELY_OVERLOADED)) {
910 nativelyoverloaded = false;
912 for (j = i + 1; j < c->methodscount; j++) {
913 m2 = &(c->methods[j]);
915 if (!(m2->flags & ACC_NATIVE))
918 if (m->name == m2->name) {
919 m2->flags |= ACC_NATIVELY_OVERLOADED;
920 nativelyoverloaded = true;
925 if (nativelyoverloaded == true)
926 m->flags |= ACC_NATIVELY_OVERLOADED;
929 for (i = 0; i < c->methodscount; i++) {
930 m = &(c->methods[i]);
932 if (m->flags & ACC_NATIVE)
936 chain_free(ident_chain);
938 fprintf(file, "#endif\n\n");
944 /******** print classname, '$' used to seperate inner-class name ***********/
946 void print_classname(classinfo *clazz)
948 utf *u = clazz->name;
949 char *endpos = u->text + u->blength;
950 char *utf_ptr = u->text;
953 while (utf_ptr < endpos) {
954 if ((c = utf_nextu2(&utf_ptr)) == '_')
961 /* jvmti releated functions ************************************************/
963 #if defined(ENABLE_JVMTI)
964 void jvmti_ThreadStartEnd(int ev) {;}
965 void jvmti_ClassLoadPrepare(bool prepared, classinfo *c) {;}
966 void jvmti_MonitorContendedEntering(bool entered, jobject obj) {;}
972 * These are local overrides for various environment variables in Emacs.
973 * Please do not remove this and leave it at the end of the file, where
974 * Emacs will automagically detect them.
975 * ---------------------------------------------------------------------
978 * indent-tabs-mode: t
982 * vim:noexpandtab:sw=4:ts=4: