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 5739 2006-10-11 19:43:12Z edwin $
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 */
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_print_trace(java_objectheader *xptr) {}
186 java_objectarray *stacktrace_getClassContext() { return NULL; }
187 void code_free_code_of_method(methodinfo *m) {}
190 /* exception functions ********************************************************/
192 /* these should not be called */
194 void throw_main_exception_exit(void) { assert(0); }
195 void throw_exception(void) { assert(0); }
196 void throw_exception_exit(void) { assert(0); }
198 void exceptions_throw_verifyerror(methodinfo *m, const char *message)
203 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
207 /* keep compiler happy */
213 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
217 fprintf(stderr, "%s: ", exception);
219 va_start(ap, message);
220 vfprintf(stderr, message, ap);
229 void exceptions_throw_outofmemory_exit(void)
231 fprintf(stderr, "java.lang.InternalError: Out of memory\n");
236 java_objectheader *new_exception(const char *classname)
238 fprintf(stderr, "%s\n", classname);
241 /* keep compiler happy */
247 java_objectheader *new_exception_message(const char *classname, const char *message)
249 fprintf(stderr, "%s: %s\n", classname, message);
252 /* keep compiler happy */
258 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
260 fprintf(stderr, "%s: ", classname);
261 utf_display_printable_ascii(message);
266 /* keep compiler happy */
272 java_objectheader *new_exception_javastring(const char *classname,
273 java_lang_String *message)
275 fprintf(stderr, "%s: ", classname);
276 /* TODO print message */
281 /* keep compiler happy */
287 void exceptions_throw_abstractmethoderror(void)
289 fprintf(stderr, "java.lang.AbstractMethodError\n");
295 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
299 utf_display_printable_ascii(c->name);
300 fprintf(stderr, ": ");
302 va_start(ap, message);
303 vfprintf(stderr, message, ap);
310 /* keep compiler happy */
316 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
320 va_start(ap, message);
321 (void) new_classformaterror(c, message, ap);
326 java_objectheader *new_classnotfoundexception(utf *name)
328 fprintf(stderr, "java.lang.ClassNotFoundException: ");
329 utf_fprint_printable_ascii(stderr, name);
334 /* keep compiler happy */
340 java_objectheader *new_noclassdeffounderror(utf *name)
342 fprintf(stderr, "java.lang.NoClassDefFoundError: ");
343 utf_fprint_printable_ascii(stderr, name);
348 /* keep compiler happy */
354 java_objectheader *exceptions_new_linkageerror(const char *message,
357 fprintf(stderr, "java.lang.LinkageError: %s",message);
359 utf_fprint_printable_ascii_classname(stderr, c->name);
365 /* keep compiler happy */
370 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
371 utf *name, utf *desc)
373 fprintf(stderr, "java.lang.NoSuchMethodError: ");
374 utf_fprint_printable_ascii(stderr, c->name);
375 fprintf(stderr, ".");
376 utf_fprint_printable_ascii(stderr, name);
377 utf_fprint_printable_ascii(stderr, desc);
382 /* keep compiler happy */
388 void exceptions_throw_nosuchmethoderror(classinfo *c, utf *name, utf *desc)
390 (void) exceptions_new_nosuchmethoderror(c, name, desc);
394 java_objectheader *new_internalerror(const char *message, ...)
398 fprintf(stderr, "%s: ", string_java_lang_InternalError);
400 va_start(ap, message);
401 vfprintf(stderr, message, ap);
406 /* keep compiler happy */
412 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
416 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
418 utf_display_printable_ascii(c->name);
419 fprintf(stderr, ": ");
421 va_start(ap, message);
422 vfprintf(stderr, message, ap);
427 /* keep compiler happy */
433 void exceptions_throw_arrayindexoutofboundsexception(void)
435 fprintf(stderr, "%s", string_java_lang_ArrayIndexOutOfBoundsException);
440 java_objectheader *exceptions_new_arraystoreexception(void)
442 fprintf(stderr, "%s", string_java_lang_ArrayStoreException);
445 /* keep compiler happy */
451 void exceptions_throw_arraystoreexception(void)
453 (void) exceptions_new_arraystoreexception();
457 java_objectheader *exceptions_throw_illegalmonitorstateexception(void)
459 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
462 /* keep compiler happy */
468 java_objectheader *new_negativearraysizeexception(void)
470 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
473 /* keep compiler happy */
479 void exceptions_throw_negativearraysizeexception(void)
481 (void) new_negativearraysizeexception();
485 java_objectheader *new_nullpointerexception(void)
487 fprintf(stderr, "%s", string_java_lang_NullPointerException);
490 /* keep compiler happy */
496 void exceptions_throw_nullpointerexception(void)
498 (void) new_nullpointerexception();
502 void classnotfoundexception_to_noclassdeffounderror(void)
506 /* machine dependent stuff ****************************************************/
508 #if defined(ENABLE_THREADS)
509 critical_section_node_t asm_criticalsections;
510 void thread_restartcriticalsection(ucontext_t *uc) {}
513 void md_param_alloc(methoddesc *md) {}
516 #if defined(ENABLE_INTRP)
517 void print_dynamic_super_statistics(void) {}
521 /************************ global variables **********************/
523 #define ACC_NATIVELY_OVERLOADED 0x10000000
525 chain *ident_chain; /* chain with method and field names in current class */
527 static u4 outputsize;
528 static bool dopadding;
531 static void printIDpart(int c)
533 if ((c >= 'a' && c <= 'z') ||
534 (c >= 'A' && c <= 'Z') ||
535 (c >= '0' && c <= '9') ||
545 char *utf_ptr = u->text;
548 for (i = 0; i < utf_get_number_of_u2s(u); i++)
549 printIDpart(utf_nextu2(&utf_ptr));
553 static void addoutputsize (int len)
556 if (!dopadding) return;
558 newsize = ALIGN(outputsize, len);
560 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
561 outputsize = newsize;
565 void printOverloadPart(utf *desc)
567 char *utf_ptr=desc->text;
572 while ((c = utf_nextu2(&utf_ptr)) != ')') {
582 fprintf(file, "%c", (char) c);
589 while ((c = utf_nextu2(&utf_ptr)) != ';')
596 log_text("invalid method descriptor");
602 static char *printtype(char *utf_ptr)
606 switch (utf_nextu2(&utf_ptr)) {
607 case 'V': fprintf (file, "void");
613 case 'Z': addoutputsize (4);
614 fprintf (file, "s4");
616 case 'J': addoutputsize (8);
617 fprintf (file, "s8");
619 case 'F': addoutputsize (4);
620 fprintf (file, "float");
622 case 'D': addoutputsize (8);
623 fprintf (file, "double");
626 addoutputsize ( sizeof(java_arrayheader*) );
627 switch (utf_nextu2(&utf_ptr)) {
628 case 'I': fprintf (file, "java_intarray*"); break;
629 case 'J': fprintf (file, "java_longarray*"); break;
630 case 'Z': fprintf (file, "java_booleanarray*"); break;
631 case 'B': fprintf (file, "java_bytearray*"); break;
632 case 'S': fprintf (file, "java_shortarray*"); break;
633 case 'C': fprintf (file, "java_chararray*"); break;
634 case 'F': fprintf (file, "java_floatarray*"); break;
635 case 'D': fprintf (file, "java_doublearray*"); break;
637 case '[': fprintf(file, "java_objectarray*");
638 while ((c = utf_nextu2(&utf_ptr)) == '[');
640 while (utf_nextu2(&utf_ptr) != ';');
643 case 'L': fprintf(file, "java_objectarray*");
644 while (utf_nextu2(&utf_ptr) != ';');
647 log_text("invalid type descriptor");
653 addoutputsize ( sizeof(java_objectheader*));
654 fprintf (file, "struct ");
655 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
660 log_text("Unknown type in field descriptor");
668 /***** determine the number of entries of a utf string in the ident chain *****/
670 static int searchidentchain_utf(utf *ident)
672 utf *u = chain_first(ident_chain); /* first element of list */
676 if (u==ident) count++; /* string found */
677 u = chain_next(ident_chain); /* next element in list */
684 /************** print structure for direct access to objects ******************/
686 static void printfields(classinfo *c)
693 addoutputsize(sizeof(java_objectheader));
694 fprintf(file, " java_objectheader header;\n");
698 printfields(c->super.cls);
700 for (i = 0; i < c->fieldscount; i++) {
703 if (!(f->flags & ACC_STATIC)) {
705 printtype(f->descriptor->text);
707 utf_fprint_printable_ascii(file, f->name);
709 /* rename multiple fieldnames */
710 if ((ident_count = searchidentchain_utf(f->name)))
711 fprintf(file, "%d", ident_count - 1);
712 chain_addlast(ident_chain, f->name);
714 fprintf(file, ";\n");
720 /***************** store prototype for native method in file ******************/
722 void printmethod(methodinfo *m)
727 /* search for return-type in descriptor */
728 utf_ptr = m->descriptor->text;
729 while (utf_nextu2(&utf_ptr) != ')');
732 fprintf(file, "\n/*\n * Class: ");
733 utf_fprint_printable_ascii(file, m->class->name);
734 fprintf(file, "\n * Method: ");
735 utf_fprint_printable_ascii(file, m->name);
736 fprintf(file, "\n * Signature: ");
737 utf_fprint_printable_ascii(file, m->descriptor);
738 fprintf(file, "\n */\n");
740 /* create prototype */
741 fprintf(file, "JNIEXPORT ");
743 fprintf(file, " JNICALL Java_");
744 printID(m->class->name);
746 chain_addlast(ident_chain, m->name);
751 /* ATTENTION: We use a dummy flag here. */
753 if (m->flags & ACC_NATIVELY_OVERLOADED)
754 printOverloadPart(m->descriptor);
756 fprintf(file, "(JNIEnv *env");
758 utf_ptr = m->descriptor->text + 1;
760 if (!(m->flags & ACC_STATIC)) {
761 fprintf(file, ", struct ");
762 printID(m->class->name);
763 fprintf(file, "* this");
766 fprintf(file, ", jclass clazz");
769 if ((*utf_ptr) != ')') fprintf(file, ", ");
771 while ((*utf_ptr) != ')') {
772 utf_ptr = printtype(utf_ptr);
773 fprintf(file, " par%d", paramnum++);
774 if ((*utf_ptr)!=')') fprintf(file, ", ");
777 fprintf(file, ");\n\n");
781 /******* remove package-name in fully-qualified classname *********************/
783 void gen_header_filename(char *buffer, utf *u)
787 for (i = 0; i < utf_get_number_of_u2s(u); i++) {
788 if ((u->text[i] == '/') || (u->text[i] == '$')) {
789 buffer[i] = '_'; /* convert '$' and '/' to '_' */
792 buffer[i] = u->text[i];
795 buffer[utf_get_number_of_u2s(u)] = '\0';
799 /* create headerfile for classes and store native methods in chain ************/
801 void headerfile_generate(classinfo *c, char *opt_directory)
803 char header_filename[1024] = "";
804 char classname[1024];
805 char uclassname[1024];
810 bool nativelyoverloaded;
812 /* prevent compiler warnings */
814 nativelyoverloaded = false;
816 /* open headerfile for class */
817 gen_header_filename(classname, c->name);
819 /* create chain for renaming fields */
820 ident_chain = chain_new();
823 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
826 sprintf(header_filename, "%s.h", classname);
829 file = fopen(header_filename, "w");
831 log_text("Can not open file to store header information");
835 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
837 /* convert to uppercase */
838 for (i = 0; classname[i]; i++) {
839 uclassname[i] = toupper(classname[i]);
841 uclassname[i] = '\0';
843 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
845 /* create structure for direct access to objects */
846 fprintf(file, "/* Structure information for class: ");
847 utf_fprint_printable_ascii(file, c->name);
848 fprintf(file, " */\n\n");
849 fprintf(file, "typedef struct ");
851 fprintf(file, " {\n");
859 fprintf(file, ";\n\n");
861 /* create chain for renaming overloaded methods */
862 chain_free(ident_chain);
863 ident_chain = chain_new();
865 /* create method-prototypes */
867 /* find overloaded methods */
869 for (i = 0; i < c->methodscount; i++) {
870 m = &(c->methods[i]);
872 if (!(m->flags & ACC_NATIVE))
875 /* We use a dummy flag here. */
877 if (!(m->flags & ACC_NATIVELY_OVERLOADED)) {
878 nativelyoverloaded = false;
880 for (j = i + 1; j < c->methodscount; j++) {
881 m2 = &(c->methods[j]);
883 if (!(m2->flags & ACC_NATIVE))
886 if (m->name == m2->name) {
887 m2->flags |= ACC_NATIVELY_OVERLOADED;
888 nativelyoverloaded = true;
893 if (nativelyoverloaded == true)
894 m->flags |= ACC_NATIVELY_OVERLOADED;
897 for (i = 0; i < c->methodscount; i++) {
898 m = &(c->methods[i]);
900 if (m->flags & ACC_NATIVE)
904 chain_free(ident_chain);
906 fprintf(file, "#endif\n\n");
912 /******** print classname, '$' used to seperate inner-class name ***********/
914 void print_classname(classinfo *clazz)
916 utf *u = clazz->name;
917 char *endpos = u->text + u->blength;
918 char *utf_ptr = u->text;
921 while (utf_ptr < endpos) {
922 if ((c = utf_nextu2(&utf_ptr)) == '_')
929 /* jvmti releated functions ************************************************/
931 #if defined(ENABLE_JVMTI)
932 void jvmti_ThreadStartEnd(int ev) {;}
933 void jvmti_ClassLoadPrepare(bool prepared, classinfo *c) {;}
934 void jvmti_MonitorContendedEntering(bool entered, jobject obj) {;}
940 * These are local overrides for various environment variables in Emacs.
941 * Please do not remove this and leave it at the end of the file, where
942 * Emacs will automagically detect them.
943 * ---------------------------------------------------------------------
946 * indent-tabs-mode: t
950 * vim:noexpandtab:sw=4:ts=4: