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 5195 2006-07-31 15:26:10Z 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 */
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) {}
183 void exceptions_print_exception(java_objectheader *xptr) {}
184 void stacktrace_print_trace(java_objectheader *xptr) {}
186 void code_free_code_of_method(methodinfo *m) {}
189 /* exception functions ********************************************************/
191 /* these should not be called */
193 void throw_main_exception_exit(void) { assert(0); }
194 void throw_exception(void) { assert(0); }
195 void throw_exception_exit(void) { assert(0); }
197 void exceptions_throw_verifyerror(methodinfo *m, const char *message)
202 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
206 /* keep compiler happy */
212 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
216 fprintf(stderr, "%s: ", exception);
218 va_start(ap, message);
219 vfprintf(stderr, message, ap);
228 void exceptions_throw_outofmemory_exit(void)
230 fprintf(stderr, "java.lang.InternalError: Out of memory\n");
235 java_objectheader *new_exception(const char *classname)
237 fprintf(stderr, "%s\n", classname);
240 /* keep compiler happy */
246 java_objectheader *new_exception_message(const char *classname, const char *message)
248 fprintf(stderr, "%s: %s\n", classname, message);
251 /* keep compiler happy */
257 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
259 fprintf(stderr, "%s: ", classname);
260 utf_display_printable_ascii(message);
265 /* keep compiler happy */
271 java_objectheader *new_exception_javastring(const char *classname,
272 java_lang_String *message)
274 fprintf(stderr, "%s: ", classname);
275 /* TODO print message */
280 /* keep compiler happy */
286 void exceptions_throw_abstractmethoderror(void)
288 fprintf(stderr, "java.lang.AbstractMethodError\n");
294 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
298 utf_display_printable_ascii(c->name);
299 fprintf(stderr, ": ");
301 va_start(ap, message);
302 vfprintf(stderr, message, ap);
309 /* keep compiler happy */
315 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
319 va_start(ap, message);
320 (void) new_classformaterror(c, message, ap);
325 java_objectheader *new_classnotfoundexception(utf *name)
327 fprintf(stderr, "java.lang.ClassNotFoundException: ");
328 utf_fprint_printable_ascii(stderr, name);
333 /* keep compiler happy */
339 java_objectheader *new_noclassdeffounderror(utf *name)
341 fprintf(stderr, "java.lang.NoClassDefFoundError: ");
342 utf_fprint_printable_ascii(stderr, name);
347 /* keep compiler happy */
353 java_objectheader *exceptions_new_linkageerror(const char *message,
356 fprintf(stderr, "java.lang.LinkageError: %s",message);
358 utf_fprint_printable_ascii_classname(stderr, c->name);
364 /* keep compiler happy */
369 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
370 utf *name, utf *desc)
372 fprintf(stderr, "java.lang.NoSuchMethodError: ");
373 utf_fprint_printable_ascii(stderr, c->name);
374 fprintf(stderr, ".");
375 utf_fprint_printable_ascii(stderr, name);
376 utf_fprint_printable_ascii(stderr, desc);
381 /* keep compiler happy */
387 void exceptions_throw_nosuchmethoderror(classinfo *c, utf *name, utf *desc)
389 (void) exceptions_new_nosuchmethoderror(c, name, desc);
393 java_objectheader *new_internalerror(const char *message, ...)
397 fprintf(stderr, "%s: ", string_java_lang_InternalError);
399 va_start(ap, message);
400 vfprintf(stderr, message, ap);
405 /* keep compiler happy */
411 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
415 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
417 utf_display_printable_ascii(c->name);
418 fprintf(stderr, ": ");
420 va_start(ap, message);
421 vfprintf(stderr, message, ap);
426 /* keep compiler happy */
432 java_objectheader *new_illegalmonitorstateexception(void)
434 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
437 /* keep compiler happy */
443 java_objectheader *new_negativearraysizeexception(void)
445 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
448 /* keep compiler happy */
454 void exceptions_throw_negativearraysizeexception(void)
456 (void) new_negativearraysizeexception();
460 java_objectheader *new_nullpointerexception(void)
462 fprintf(stderr, "%s", string_java_lang_NullPointerException);
465 /* keep compiler happy */
471 void exceptions_throw_nullpointerexception(void)
473 (void) new_nullpointerexception();
477 void classnotfoundexception_to_noclassdeffounderror(void)
481 /* machine dependent stuff ****************************************************/
483 #if defined(ENABLE_THREADS)
484 critical_section_node_t asm_criticalsections;
485 void thread_restartcriticalsection(ucontext_t *uc) {}
488 void md_param_alloc(methoddesc *md) {}
491 #if defined(ENABLE_INTRP)
492 void print_dynamic_super_statistics(void) {}
496 /************************ global variables **********************/
498 chain *ident_chain; /* chain with method and field names in current class */
500 static u4 outputsize;
501 static bool dopadding;
504 static void printIDpart(int c)
506 if ((c >= 'a' && c <= 'z') ||
507 (c >= 'A' && c <= 'Z') ||
508 (c >= '0' && c <= '9') ||
518 char *utf_ptr = u->text;
521 for (i = 0; i < utf_get_number_of_u2s(u); i++)
522 printIDpart(utf_nextu2(&utf_ptr));
526 static void addoutputsize (int len)
529 if (!dopadding) return;
531 newsize = ALIGN(outputsize, len);
533 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
534 outputsize = newsize;
538 void printOverloadPart(utf *desc)
540 char *utf_ptr=desc->text;
545 while ((c = utf_nextu2(&utf_ptr)) != ')') {
555 fprintf(file, "%c", (char) c);
562 while ((c = utf_nextu2(&utf_ptr)) != ';')
569 log_text("invalid method descriptor");
575 static char *printtype(char *utf_ptr)
579 switch (utf_nextu2(&utf_ptr)) {
580 case 'V': fprintf (file, "void");
586 case 'Z': addoutputsize (4);
587 fprintf (file, "s4");
589 case 'J': addoutputsize (8);
590 fprintf (file, "s8");
592 case 'F': addoutputsize (4);
593 fprintf (file, "float");
595 case 'D': addoutputsize (8);
596 fprintf (file, "double");
599 addoutputsize ( sizeof(java_arrayheader*) );
600 switch (utf_nextu2(&utf_ptr)) {
601 case 'I': fprintf (file, "java_intarray*"); break;
602 case 'J': fprintf (file, "java_longarray*"); break;
603 case 'Z': fprintf (file, "java_booleanarray*"); break;
604 case 'B': fprintf (file, "java_bytearray*"); break;
605 case 'S': fprintf (file, "java_shortarray*"); break;
606 case 'C': fprintf (file, "java_chararray*"); break;
607 case 'F': fprintf (file, "java_floatarray*"); break;
608 case 'D': fprintf (file, "java_doublearray*"); break;
610 case '[': fprintf(file, "java_objectarray*");
611 while ((c = utf_nextu2(&utf_ptr)) == '[');
613 while (utf_nextu2(&utf_ptr) != ';');
616 case 'L': fprintf(file, "java_objectarray*");
617 while (utf_nextu2(&utf_ptr) != ';');
620 log_text("invalid type descriptor");
626 addoutputsize ( sizeof(java_objectheader*));
627 fprintf (file, "struct ");
628 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
633 log_text("Unknown type in field descriptor");
641 /***** determine the number of entries of a utf string in the ident chain *****/
643 static int searchidentchain_utf(utf *ident)
645 utf *u = chain_first(ident_chain); /* first element of list */
649 if (u==ident) count++; /* string found */
650 u = chain_next(ident_chain); /* next element in list */
657 /************** print structure for direct access to objects ******************/
659 static void printfields(classinfo *c)
666 addoutputsize(sizeof(java_objectheader));
667 fprintf(file, " java_objectheader header;\n");
671 printfields(c->super.cls);
673 for (i = 0; i < c->fieldscount; i++) {
676 if (!(f->flags & ACC_STATIC)) {
678 printtype(f->descriptor->text);
680 utf_fprint_printable_ascii(file, f->name);
682 /* rename multiple fieldnames */
683 if ((ident_count = searchidentchain_utf(f->name)))
684 fprintf(file, "%d", ident_count - 1);
685 chain_addlast(ident_chain, f->name);
687 fprintf(file, ";\n");
693 /***************** store prototype for native method in file ******************/
695 void printmethod(methodinfo *m)
700 /* search for return-type in descriptor */
701 utf_ptr = m->descriptor->text;
702 while (utf_nextu2(&utf_ptr) != ')');
705 fprintf(file, "\n/*\n * Class: ");
706 utf_fprint_printable_ascii(file, m->class->name);
707 fprintf(file, "\n * Method: ");
708 utf_fprint_printable_ascii(file, m->name);
709 fprintf(file, "\n * Signature: ");
710 utf_fprint_printable_ascii(file, m->descriptor);
711 fprintf(file, "\n */\n");
713 /* create prototype */
714 fprintf(file, "JNIEXPORT ");
716 fprintf(file, " JNICALL Java_");
717 printID(m->class->name);
719 chain_addlast(ident_chain, m->name);
724 /* ATTENTION: We use the methodinfo's stackcount variable as
725 nativelyoverloaded, so we can save some space during
729 printOverloadPart(m->descriptor);
731 fprintf(file, "(JNIEnv *env");
733 utf_ptr = m->descriptor->text + 1;
735 if (!(m->flags & ACC_STATIC)) {
736 fprintf(file, ", struct ");
737 printID(m->class->name);
738 fprintf(file, "* this");
741 fprintf(file, ", jclass clazz");
744 if ((*utf_ptr) != ')') fprintf(file, ", ");
746 while ((*utf_ptr) != ')') {
747 utf_ptr = printtype(utf_ptr);
748 fprintf(file, " par%d", paramnum++);
749 if ((*utf_ptr)!=')') fprintf(file, ", ");
752 fprintf(file, ");\n\n");
756 /******* remove package-name in fully-qualified classname *********************/
758 void gen_header_filename(char *buffer, utf *u)
762 for (i = 0; i < utf_get_number_of_u2s(u); i++) {
763 if ((u->text[i] == '/') || (u->text[i] == '$')) {
764 buffer[i] = '_'; /* convert '$' and '/' to '_' */
767 buffer[i] = u->text[i];
770 buffer[utf_get_number_of_u2s(u)] = '\0';
774 /* create headerfile for classes and store native methods in chain ************/
776 void headerfile_generate(classinfo *c, char *opt_directory)
778 char header_filename[1024] = "";
779 char classname[1024];
780 char uclassname[1024];
785 bool nativelyoverloaded;
787 /* prevent compiler warnings */
789 nativelyoverloaded = false;
791 /* open headerfile for class */
792 gen_header_filename(classname, c->name);
794 /* create chain for renaming fields */
795 ident_chain = chain_new();
798 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
801 sprintf(header_filename, "%s.h", classname);
804 file = fopen(header_filename, "w");
806 log_text("Can not open file to store header information");
810 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
812 /* convert to uppercase */
813 for (i = 0; classname[i]; i++) {
814 uclassname[i] = toupper(classname[i]);
816 uclassname[i] = '\0';
818 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
820 /* create structure for direct access to objects */
821 fprintf(file, "/* Structure information for class: ");
822 utf_fprint_printable_ascii(file, c->name);
823 fprintf(file, " */\n\n");
824 fprintf(file, "typedef struct ");
826 fprintf(file, " {\n");
834 fprintf(file, ";\n\n");
836 /* create chain for renaming overloaded methods */
837 chain_free(ident_chain);
838 ident_chain = chain_new();
840 /* create method-prototypes */
842 /* find overloaded methods */
844 for (i = 0; i < c->methodscount; i++) {
845 m = &(c->methods[i]);
847 if (!(m->flags & ACC_NATIVE))
850 /* We use the methodinfo's stackcount variable as
851 nativelyoverloaded, so we can save some space during
854 if (!m->stackcount) {
855 nativelyoverloaded = false;
857 for (j = i + 1; j < c->methodscount; j++) {
858 m2 = &(c->methods[j]);
860 if (!(m2->flags & ACC_NATIVE))
863 if (m->name == m2->name) {
864 m2->stackcount = true;
865 nativelyoverloaded = true;
870 m->stackcount = nativelyoverloaded;
873 for (i = 0; i < c->methodscount; i++) {
874 m = &(c->methods[i]);
876 if (m->flags & ACC_NATIVE)
880 chain_free(ident_chain);
882 fprintf(file, "#endif\n\n");
888 /******** print classname, '$' used to seperate inner-class name ***********/
890 void print_classname(classinfo *clazz)
892 utf *u = clazz->name;
893 char *endpos = u->text + u->blength;
894 char *utf_ptr = u->text;
897 while (utf_ptr < endpos) {
898 if ((c = utf_nextu2(&utf_ptr)) == '_')
905 /* jvmti releated functions ************************************************/
907 #if defined(ENABLE_JVMTI)
908 void jvmti_ThreadStartEnd(int ev) {;}
909 void jvmti_ClassLoadPrepare(bool prepared, classinfo *c) {;}
910 void jvmti_MonitorContendedEntering(bool entered, jobject obj) {;}
916 * These are local overrides for various environment variables in Emacs.
917 * Please do not remove this and leave it at the end of the file, where
918 * Emacs will automagically detect them.
919 * ---------------------------------------------------------------------
922 * indent-tabs-mode: t
926 * vim:noexpandtab:sw=4:ts=4: