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
34 $Id: headers.c 5810 2006-10-20 13:54:54Z twisti $
49 #if defined(ENABLE_THREADS)
50 # if defined(__DARWIN__)
53 # include <ucontext.h>
57 #include "mm/memory.h"
58 #include "native/include/java_lang_String.h"
59 #include "native/include/java_lang_Throwable.h"
60 #include "toolbox/chain.h"
61 #include "toolbox/logging.h"
62 #include "vm/builtin.h"
64 #include "vm/global.h"
65 #include "vm/method.h"
66 #include "vm/loader.h"
67 #include "vm/options.h"
68 #include "vm/stringlocal.h"
69 #include "vm/jit/asmpart.h"
72 /* Invocation API variables ***************************************************/
74 _Jv_JavaVM *_Jv_jvm; /* denotes a Java VM */
75 char *_Jv_bootclasspath;
77 #if defined(ENABLE_INTRP)
78 /* dummy interpreter stack to keep the compiler happy */
84 /* for raising exceptions from native methods *********************************/
86 #if !defined(ENABLE_THREADS)
87 java_objectheader *_no_threads_exceptionptr = NULL;
91 /* replace some non-vmcore functions ******************************************/
93 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
96 /* return something different than NULL, otherwise we get an exception */
98 return (functionptr) 1;
101 java_objectheader *native_new_and_init(classinfo *c) { return NULL; }
102 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s) { return NULL; }
103 java_objectheader *native_new_and_init_int(classinfo *c, s4 i) { return NULL; }
104 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t) { return NULL; }
107 java_objectheader *vm_call_method(methodinfo *m, java_objectheader *o, ...)
110 void vm_abort(const char *text, ...)
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 codeinfo *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) {}
146 void asm_abstractmethoderror(void) {}
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) {}
156 u8 asm_get_cycle_count(void)
162 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
167 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
169 return typecheck_TRUE;
172 void typeinfo_init_classinfo(typeinfo *info,classinfo *c)
176 bool typeinfo_init_class(typeinfo *info,classref_or_classinfo c)
181 void typeinfo_print(FILE *file,typeinfo *info,int indent) {}
182 void typeinfo_print_short(FILE *file,typeinfo *info) {}
184 void exceptions_print_exception(java_objectheader *xptr) {}
185 void stacktrace_dump_trace(threadobject *thread) {}
186 void stacktrace_print_trace(java_objectheader *xptr) {}
187 java_objectarray *stacktrace_getClassContext() { return NULL; }
188 void code_free_code_of_method(methodinfo *m) {}
191 /* exception functions ********************************************************/
193 /* these should not be called */
195 void throw_main_exception_exit(void) { assert(0); }
196 void throw_exception(void) { assert(0); }
197 void throw_exception_exit(void) { assert(0); }
199 void exceptions_throw_verifyerror(methodinfo *m, const char *message)
204 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
208 /* keep compiler happy */
214 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
218 fprintf(stderr, "%s: ", exception);
220 va_start(ap, message);
221 vfprintf(stderr, message, ap);
230 void exceptions_throw_outofmemory_exit(void)
232 fprintf(stderr, "java.lang.InternalError: Out of memory\n");
237 java_objectheader *new_exception(const char *classname)
239 fprintf(stderr, "%s\n", classname);
242 /* keep compiler happy */
248 java_objectheader *new_exception_message(const char *classname, const char *message)
250 fprintf(stderr, "%s: %s\n", classname, message);
253 /* keep compiler happy */
259 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
261 fprintf(stderr, "%s: ", classname);
262 utf_display_printable_ascii(message);
267 /* keep compiler happy */
273 java_objectheader *new_exception_javastring(const char *classname,
274 java_lang_String *message)
276 fprintf(stderr, "%s: ", classname);
277 /* TODO print message */
282 /* keep compiler happy */
288 void exceptions_throw_abstractmethoderror(void)
290 fprintf(stderr, "java.lang.AbstractMethodError\n");
296 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
300 utf_display_printable_ascii(c->name);
301 fprintf(stderr, ": ");
303 va_start(ap, message);
304 vfprintf(stderr, message, ap);
311 /* keep compiler happy */
317 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
321 va_start(ap, message);
322 (void) new_classformaterror(c, message, ap);
327 java_objectheader *new_classnotfoundexception(utf *name)
329 fprintf(stderr, "java.lang.ClassNotFoundException: ");
330 utf_fprint_printable_ascii(stderr, name);
335 /* keep compiler happy */
341 java_objectheader *new_noclassdeffounderror(utf *name)
343 fprintf(stderr, "java.lang.NoClassDefFoundError: ");
344 utf_fprint_printable_ascii(stderr, name);
349 /* keep compiler happy */
355 java_objectheader *exceptions_new_linkageerror(const char *message,
358 fprintf(stderr, "java.lang.LinkageError: %s",message);
360 utf_fprint_printable_ascii_classname(stderr, c->name);
366 /* keep compiler happy */
371 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
372 utf *name, utf *desc)
374 fprintf(stderr, "java.lang.NoSuchMethodError: ");
375 utf_fprint_printable_ascii(stderr, c->name);
376 fprintf(stderr, ".");
377 utf_fprint_printable_ascii(stderr, name);
378 utf_fprint_printable_ascii(stderr, desc);
383 /* keep compiler happy */
389 void exceptions_throw_nosuchmethoderror(classinfo *c, utf *name, utf *desc)
391 (void) exceptions_new_nosuchmethoderror(c, name, desc);
395 java_objectheader *new_internalerror(const char *message, ...)
399 fprintf(stderr, "%s: ", string_java_lang_InternalError);
401 va_start(ap, message);
402 vfprintf(stderr, message, ap);
407 /* keep compiler happy */
413 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
417 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
419 utf_display_printable_ascii(c->name);
420 fprintf(stderr, ": ");
422 va_start(ap, message);
423 vfprintf(stderr, message, ap);
428 /* keep compiler happy */
434 void exceptions_throw_arrayindexoutofboundsexception(void)
436 fprintf(stderr, "%s", string_java_lang_ArrayIndexOutOfBoundsException);
441 java_objectheader *exceptions_new_arraystoreexception(void)
443 fprintf(stderr, "%s", string_java_lang_ArrayStoreException);
446 /* keep compiler happy */
452 void exceptions_throw_arraystoreexception(void)
454 (void) exceptions_new_arraystoreexception();
458 java_objectheader *exceptions_throw_illegalmonitorstateexception(void)
460 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
463 /* keep compiler happy */
469 java_objectheader *new_negativearraysizeexception(void)
471 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
474 /* keep compiler happy */
480 void exceptions_throw_negativearraysizeexception(void)
482 (void) new_negativearraysizeexception();
486 java_objectheader *new_nullpointerexception(void)
488 fprintf(stderr, "%s", string_java_lang_NullPointerException);
491 /* keep compiler happy */
497 void exceptions_throw_nullpointerexception(void)
499 (void) new_nullpointerexception();
503 void classnotfoundexception_to_noclassdeffounderror(void)
507 /* machine dependent stuff ****************************************************/
509 #if defined(ENABLE_THREADS)
510 critical_section_node_t asm_criticalsections;
511 void thread_restartcriticalsection(ucontext_t *uc) {}
514 void md_param_alloc(methoddesc *md) {}
517 #if defined(ENABLE_INTRP)
518 void print_dynamic_super_statistics(void) {}
522 /************************ global variables **********************/
524 #define ACC_NATIVELY_OVERLOADED 0x10000000
526 chain *ident_chain; /* chain with method and field names in current class */
528 static u4 outputsize;
529 static bool dopadding;
532 static void printIDpart(int c)
534 if ((c >= 'a' && c <= 'z') ||
535 (c >= 'A' && c <= 'Z') ||
536 (c >= '0' && c <= '9') ||
546 char *utf_ptr = u->text;
549 for (i = 0; i < utf_get_number_of_u2s(u); i++)
550 printIDpart(utf_nextu2(&utf_ptr));
554 static void addoutputsize (int len)
557 if (!dopadding) return;
559 newsize = ALIGN(outputsize, len);
561 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
562 outputsize = newsize;
566 void printOverloadPart(utf *desc)
568 char *utf_ptr=desc->text;
573 while ((c = utf_nextu2(&utf_ptr)) != ')') {
583 fprintf(file, "%c", (char) c);
590 while ((c = utf_nextu2(&utf_ptr)) != ';')
597 log_text("invalid method descriptor");
603 static char *printtype(char *utf_ptr)
607 switch (utf_nextu2(&utf_ptr)) {
608 case 'V': fprintf (file, "void");
614 case 'Z': addoutputsize (4);
615 fprintf (file, "s4");
617 case 'J': addoutputsize (8);
618 fprintf (file, "s8");
620 case 'F': addoutputsize (4);
621 fprintf (file, "float");
623 case 'D': addoutputsize (8);
624 fprintf (file, "double");
627 addoutputsize ( sizeof(java_arrayheader*) );
628 switch (utf_nextu2(&utf_ptr)) {
629 case 'I': fprintf (file, "java_intarray*"); break;
630 case 'J': fprintf (file, "java_longarray*"); break;
631 case 'Z': fprintf (file, "java_booleanarray*"); break;
632 case 'B': fprintf (file, "java_bytearray*"); break;
633 case 'S': fprintf (file, "java_shortarray*"); break;
634 case 'C': fprintf (file, "java_chararray*"); break;
635 case 'F': fprintf (file, "java_floatarray*"); break;
636 case 'D': fprintf (file, "java_doublearray*"); break;
638 case '[': fprintf(file, "java_objectarray*");
639 while ((c = utf_nextu2(&utf_ptr)) == '[');
641 while (utf_nextu2(&utf_ptr) != ';');
644 case 'L': fprintf(file, "java_objectarray*");
645 while (utf_nextu2(&utf_ptr) != ';');
648 log_text("invalid type descriptor");
654 addoutputsize ( sizeof(java_objectheader*));
655 fprintf (file, "struct ");
656 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
661 log_text("Unknown type in field descriptor");
669 /***** determine the number of entries of a utf string in the ident chain *****/
671 static int searchidentchain_utf(utf *ident)
673 utf *u = chain_first(ident_chain); /* first element of list */
677 if (u==ident) count++; /* string found */
678 u = chain_next(ident_chain); /* next element in list */
685 /************** print structure for direct access to objects ******************/
687 static void printfields(classinfo *c)
694 addoutputsize(sizeof(java_objectheader));
695 fprintf(file, " java_objectheader header;\n");
699 printfields(c->super.cls);
701 for (i = 0; i < c->fieldscount; i++) {
704 if (!(f->flags & ACC_STATIC)) {
706 printtype(f->descriptor->text);
708 utf_fprint_printable_ascii(file, f->name);
710 /* rename multiple fieldnames */
711 if ((ident_count = searchidentchain_utf(f->name)))
712 fprintf(file, "%d", ident_count - 1);
713 chain_addlast(ident_chain, f->name);
715 fprintf(file, ";\n");
721 /***************** store prototype for native method in file ******************/
723 void printmethod(methodinfo *m)
728 /* search for return-type in descriptor */
729 utf_ptr = m->descriptor->text;
730 while (utf_nextu2(&utf_ptr) != ')');
733 fprintf(file, "\n/*\n * Class: ");
734 utf_fprint_printable_ascii(file, m->class->name);
735 fprintf(file, "\n * Method: ");
736 utf_fprint_printable_ascii(file, m->name);
737 fprintf(file, "\n * Signature: ");
738 utf_fprint_printable_ascii(file, m->descriptor);
739 fprintf(file, "\n */\n");
741 /* create prototype */
742 fprintf(file, "JNIEXPORT ");
744 fprintf(file, " JNICALL Java_");
745 printID(m->class->name);
747 chain_addlast(ident_chain, m->name);
752 /* ATTENTION: We use a dummy flag here. */
754 if (m->flags & ACC_NATIVELY_OVERLOADED)
755 printOverloadPart(m->descriptor);
757 fprintf(file, "(JNIEnv *env");
759 utf_ptr = m->descriptor->text + 1;
761 if (!(m->flags & ACC_STATIC)) {
762 fprintf(file, ", struct ");
763 printID(m->class->name);
764 fprintf(file, "* this");
767 fprintf(file, ", jclass clazz");
770 if ((*utf_ptr) != ')') fprintf(file, ", ");
772 while ((*utf_ptr) != ')') {
773 utf_ptr = printtype(utf_ptr);
774 fprintf(file, " par%d", paramnum++);
775 if ((*utf_ptr)!=')') fprintf(file, ", ");
778 fprintf(file, ");\n\n");
782 /******* remove package-name in fully-qualified classname *********************/
784 void gen_header_filename(char *buffer, utf *u)
788 for (i = 0; i < utf_get_number_of_u2s(u); i++) {
789 if ((u->text[i] == '/') || (u->text[i] == '$')) {
790 buffer[i] = '_'; /* convert '$' and '/' to '_' */
793 buffer[i] = u->text[i];
796 buffer[utf_get_number_of_u2s(u)] = '\0';
800 /* create headerfile for classes and store native methods in chain ************/
802 void headerfile_generate(classinfo *c, char *opt_directory)
804 char header_filename[1024] = "";
805 char classname[1024];
806 char uclassname[1024];
811 bool nativelyoverloaded;
813 /* prevent compiler warnings */
815 nativelyoverloaded = false;
817 /* open headerfile for class */
818 gen_header_filename(classname, c->name);
820 /* create chain for renaming fields */
821 ident_chain = chain_new();
824 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
827 sprintf(header_filename, "%s.h", classname);
830 file = fopen(header_filename, "w");
832 log_text("Can not open file to store header information");
836 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
838 /* convert to uppercase */
839 for (i = 0; classname[i]; i++) {
840 uclassname[i] = toupper(classname[i]);
842 uclassname[i] = '\0';
844 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
846 /* create structure for direct access to objects */
847 fprintf(file, "/* Structure information for class: ");
848 utf_fprint_printable_ascii(file, c->name);
849 fprintf(file, " */\n\n");
850 fprintf(file, "typedef struct ");
852 fprintf(file, " {\n");
860 fprintf(file, ";\n\n");
862 /* create chain for renaming overloaded methods */
863 chain_free(ident_chain);
864 ident_chain = chain_new();
866 /* create method-prototypes */
868 /* find overloaded methods */
870 for (i = 0; i < c->methodscount; i++) {
871 m = &(c->methods[i]);
873 if (!(m->flags & ACC_NATIVE))
876 /* We use a dummy flag here. */
878 if (!(m->flags & ACC_NATIVELY_OVERLOADED)) {
879 nativelyoverloaded = false;
881 for (j = i + 1; j < c->methodscount; j++) {
882 m2 = &(c->methods[j]);
884 if (!(m2->flags & ACC_NATIVE))
887 if (m->name == m2->name) {
888 m2->flags |= ACC_NATIVELY_OVERLOADED;
889 nativelyoverloaded = true;
894 if (nativelyoverloaded == true)
895 m->flags |= ACC_NATIVELY_OVERLOADED;
898 for (i = 0; i < c->methodscount; i++) {
899 m = &(c->methods[i]);
901 if (m->flags & ACC_NATIVE)
905 chain_free(ident_chain);
907 fprintf(file, "#endif\n\n");
913 /******** print classname, '$' used to seperate inner-class name ***********/
915 void print_classname(classinfo *clazz)
917 utf *u = clazz->name;
918 char *endpos = u->text + u->blength;
919 char *utf_ptr = u->text;
922 while (utf_ptr < endpos) {
923 if ((c = utf_nextu2(&utf_ptr)) == '_')
930 /* jvmti releated functions ************************************************/
932 #if defined(ENABLE_JVMTI)
933 void jvmti_ThreadStartEnd(int ev) {;}
934 void jvmti_ClassLoadPrepare(bool prepared, classinfo *c) {;}
935 void jvmti_MonitorContendedEntering(bool entered, jobject obj) {;}
941 * These are local overrides for various environment variables in Emacs.
942 * Please do not remove this and leave it at the end of the file, where
943 * Emacs will automagically detect them.
944 * ---------------------------------------------------------------------
947 * indent-tabs-mode: t
951 * vim:noexpandtab:sw=4:ts=4: