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 5935 2006-11-08 20:27:37Z 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;
76 #if defined(ENABLE_INTRP)
77 /* dummy interpreter stack to keep the compiler happy */
83 /* for raising exceptions from native methods *********************************/
85 #if !defined(ENABLE_THREADS)
86 java_objectheader *_no_threads_exceptionptr = NULL;
90 /* replace some non-vmcore functions ******************************************/
92 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
95 /* return something different than NULL, otherwise we get an exception */
97 return (functionptr) 1;
100 java_objectheader *native_new_and_init(classinfo *c) { return NULL; }
101 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s) { return NULL; }
102 java_objectheader *native_new_and_init_int(classinfo *c, s4 i) { return NULL; }
103 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t) { return NULL; }
106 java_objectheader *vm_call_method(methodinfo *m, java_objectheader *o, ...)
109 void vm_abort(const char *text, ...)
113 /* print the log message */
116 vfprintf(stderr, text, ap);
119 /* now abort the VM */
124 /* code patching functions */
125 void patcher_builtin_arraycheckcast(u1 *sp) {}
127 #if defined(__MIPS__)
128 long compare_and_swap(long *p, long oldval, long newval)
139 u1 *createcompilerstub(methodinfo *m) { return NULL; }
140 #if defined(ENABLE_INTRP)
141 u1 *intrp_createcompilerstub(methodinfo *m) { return NULL; }
144 codeinfo *codegen_createnativestub(functionptr f, methodinfo *m) { return NULL; }
146 void removecompilerstub(u1 *stub) {}
147 void removenativestub(u1 *stub) {}
149 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
150 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
152 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
154 void asm_handle_builtin_exception(classinfo *c) {}
156 #if defined(ENABLE_JIT)
157 void asm_abstractmethoderror(void) {}
158 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
161 #if defined(ENABLE_INTRP)
162 void intrp_asm_abstractmethoderror(void) {}
163 void intrp_asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
166 u8 asm_get_cycle_count(void)
172 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
177 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
179 return typecheck_TRUE;
182 void typeinfo_init_classinfo(typeinfo *info,classinfo *c)
186 bool typeinfo_init_class(typeinfo *info,classref_or_classinfo c)
191 void typeinfo_print(FILE *file,typeinfo *info,int indent) {}
192 void typeinfo_print_short(FILE *file,typeinfo *info) {}
194 void exceptions_print_exception(java_objectheader *xptr) {}
195 void stacktrace_dump_trace(threadobject *thread) {}
196 void stacktrace_print_trace(java_objectheader *xptr) {}
197 java_objectarray *stacktrace_getClassContext() { return NULL; }
198 void code_free_code_of_method(methodinfo *m) {}
201 /* exception functions ********************************************************/
203 /* these should not be called */
205 void throw_main_exception_exit(void) { assert(0); }
206 void throw_exception(void) { assert(0); }
207 void throw_exception_exit(void) { assert(0); }
209 void exceptions_throw_verifyerror(methodinfo *m, const char *message)
214 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
218 /* keep compiler happy */
224 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
228 fprintf(stderr, "%s: ", exception);
230 va_start(ap, message);
231 vfprintf(stderr, message, ap);
240 void exceptions_throw_outofmemory_exit(void)
242 fprintf(stderr, "java.lang.InternalError: Out of memory\n");
247 java_objectheader *new_exception(const char *classname)
249 fprintf(stderr, "%s\n", classname);
252 /* keep compiler happy */
258 java_objectheader *new_exception_message(const char *classname, const char *message)
260 fprintf(stderr, "%s: %s\n", classname, message);
263 /* keep compiler happy */
269 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
271 fprintf(stderr, "%s: ", classname);
272 utf_display_printable_ascii(message);
277 /* keep compiler happy */
283 java_objectheader *new_exception_javastring(const char *classname,
284 java_lang_String *message)
286 fprintf(stderr, "%s: ", classname);
287 /* TODO print message */
292 /* keep compiler happy */
298 void exceptions_throw_abstractmethoderror(void)
300 fprintf(stderr, "java.lang.AbstractMethodError\n");
306 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
310 utf_display_printable_ascii(c->name);
311 fprintf(stderr, ": ");
313 va_start(ap, message);
314 vfprintf(stderr, message, ap);
321 /* keep compiler happy */
327 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
331 va_start(ap, message);
332 (void) new_classformaterror(c, message, ap);
337 java_objectheader *new_classnotfoundexception(utf *name)
339 fprintf(stderr, "java.lang.ClassNotFoundException: ");
340 utf_fprint_printable_ascii(stderr, name);
345 /* keep compiler happy */
351 java_objectheader *new_noclassdeffounderror(utf *name)
353 fprintf(stderr, "java.lang.NoClassDefFoundError: ");
354 utf_fprint_printable_ascii(stderr, name);
359 /* keep compiler happy */
365 java_objectheader *exceptions_new_linkageerror(const char *message,
368 fprintf(stderr, "java.lang.LinkageError: %s",message);
370 utf_fprint_printable_ascii_classname(stderr, c->name);
376 /* keep compiler happy */
381 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
382 utf *name, utf *desc)
384 fprintf(stderr, "java.lang.NoSuchMethodError: ");
385 utf_fprint_printable_ascii(stderr, c->name);
386 fprintf(stderr, ".");
387 utf_fprint_printable_ascii(stderr, name);
388 utf_fprint_printable_ascii(stderr, desc);
393 /* keep compiler happy */
399 void exceptions_throw_nosuchmethoderror(classinfo *c, utf *name, utf *desc)
401 (void) exceptions_new_nosuchmethoderror(c, name, desc);
405 java_objectheader *new_internalerror(const char *message, ...)
409 fprintf(stderr, "%s: ", string_java_lang_InternalError);
411 va_start(ap, message);
412 vfprintf(stderr, message, ap);
417 /* keep compiler happy */
423 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
427 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
429 utf_display_printable_ascii(c->name);
430 fprintf(stderr, ": ");
432 va_start(ap, message);
433 vfprintf(stderr, message, ap);
438 /* keep compiler happy */
444 void exceptions_throw_arrayindexoutofboundsexception(void)
446 fprintf(stderr, "%s", string_java_lang_ArrayIndexOutOfBoundsException);
451 java_objectheader *exceptions_new_arraystoreexception(void)
453 fprintf(stderr, "%s", string_java_lang_ArrayStoreException);
456 /* keep compiler happy */
462 void exceptions_throw_arraystoreexception(void)
464 (void) exceptions_new_arraystoreexception();
468 java_objectheader *exceptions_throw_illegalmonitorstateexception(void)
470 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
473 /* keep compiler happy */
479 java_objectheader *new_negativearraysizeexception(void)
481 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
484 /* keep compiler happy */
490 void exceptions_throw_negativearraysizeexception(void)
492 (void) new_negativearraysizeexception();
496 java_objectheader *exceptions_new_nullpointerexception(void)
498 fprintf(stderr, "%s", string_java_lang_NullPointerException);
501 /* keep compiler happy */
507 void exceptions_throw_nullpointerexception(void)
509 (void) exceptions_new_nullpointerexception();
513 void classnotfoundexception_to_noclassdeffounderror(void)
517 /* machine dependent stuff ****************************************************/
519 #if defined(ENABLE_THREADS)
520 critical_section_node_t asm_criticalsections;
521 void thread_restartcriticalsection(ucontext_t *uc) {}
524 void md_param_alloc(methoddesc *md) {}
527 #if defined(ENABLE_INTRP)
528 void print_dynamic_super_statistics(void) {}
532 /************************ global variables **********************/
534 #define ACC_NATIVELY_OVERLOADED 0x10000000
536 chain *ident_chain; /* chain with method and field names in current class */
538 static u4 outputsize;
539 static bool dopadding;
542 static void printIDpart(int c)
544 if ((c >= 'a' && c <= 'z') ||
545 (c >= 'A' && c <= 'Z') ||
546 (c >= '0' && c <= '9') ||
556 char *utf_ptr = u->text;
559 for (i = 0; i < utf_get_number_of_u2s(u); i++)
560 printIDpart(utf_nextu2(&utf_ptr));
564 static void addoutputsize (int len)
567 if (!dopadding) return;
569 newsize = MEMORY_ALIGN(outputsize, len);
571 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
572 outputsize = newsize;
576 void printOverloadPart(utf *desc)
578 char *utf_ptr=desc->text;
583 while ((c = utf_nextu2(&utf_ptr)) != ')') {
593 fprintf(file, "%c", (char) c);
600 while ((c = utf_nextu2(&utf_ptr)) != ';')
607 log_text("invalid method descriptor");
613 static char *printtype(char *utf_ptr)
617 switch (utf_nextu2(&utf_ptr)) {
618 case 'V': fprintf (file, "void");
624 case 'Z': addoutputsize (4);
625 fprintf (file, "s4");
627 case 'J': addoutputsize (8);
628 fprintf (file, "s8");
630 case 'F': addoutputsize (4);
631 fprintf (file, "float");
633 case 'D': addoutputsize (8);
634 fprintf (file, "double");
637 addoutputsize ( sizeof(java_arrayheader*) );
638 switch (utf_nextu2(&utf_ptr)) {
639 case 'I': fprintf (file, "java_intarray*"); break;
640 case 'J': fprintf (file, "java_longarray*"); break;
641 case 'Z': fprintf (file, "java_booleanarray*"); break;
642 case 'B': fprintf (file, "java_bytearray*"); break;
643 case 'S': fprintf (file, "java_shortarray*"); break;
644 case 'C': fprintf (file, "java_chararray*"); break;
645 case 'F': fprintf (file, "java_floatarray*"); break;
646 case 'D': fprintf (file, "java_doublearray*"); break;
648 case '[': fprintf(file, "java_objectarray*");
649 while ((c = utf_nextu2(&utf_ptr)) == '[');
651 while (utf_nextu2(&utf_ptr) != ';');
654 case 'L': fprintf(file, "java_objectarray*");
655 while (utf_nextu2(&utf_ptr) != ';');
658 log_text("invalid type descriptor");
664 addoutputsize ( sizeof(java_objectheader*));
665 fprintf (file, "struct ");
666 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
671 log_text("Unknown type in field descriptor");
679 /***** determine the number of entries of a utf string in the ident chain *****/
681 static int searchidentchain_utf(utf *ident)
683 utf *u = chain_first(ident_chain); /* first element of list */
687 if (u==ident) count++; /* string found */
688 u = chain_next(ident_chain); /* next element in list */
695 /************** print structure for direct access to objects ******************/
697 static void printfields(classinfo *c)
704 addoutputsize(sizeof(java_objectheader));
705 fprintf(file, " java_objectheader header;\n");
709 printfields(c->super.cls);
711 for (i = 0; i < c->fieldscount; i++) {
714 if (!(f->flags & ACC_STATIC)) {
716 printtype(f->descriptor->text);
718 utf_fprint_printable_ascii(file, f->name);
720 /* rename multiple fieldnames */
721 if ((ident_count = searchidentchain_utf(f->name)))
722 fprintf(file, "%d", ident_count - 1);
723 chain_addlast(ident_chain, f->name);
725 fprintf(file, ";\n");
731 /***************** store prototype for native method in file ******************/
733 void printmethod(methodinfo *m)
738 /* search for return-type in descriptor */
739 utf_ptr = m->descriptor->text;
740 while (utf_nextu2(&utf_ptr) != ')');
743 fprintf(file, "\n/*\n * Class: ");
744 utf_fprint_printable_ascii(file, m->class->name);
745 fprintf(file, "\n * Method: ");
746 utf_fprint_printable_ascii(file, m->name);
747 fprintf(file, "\n * Signature: ");
748 utf_fprint_printable_ascii(file, m->descriptor);
749 fprintf(file, "\n */\n");
751 /* create prototype */
752 fprintf(file, "JNIEXPORT ");
754 fprintf(file, " JNICALL Java_");
755 printID(m->class->name);
757 chain_addlast(ident_chain, m->name);
762 /* ATTENTION: We use a dummy flag here. */
764 if (m->flags & ACC_NATIVELY_OVERLOADED)
765 printOverloadPart(m->descriptor);
767 fprintf(file, "(JNIEnv *env");
769 utf_ptr = m->descriptor->text + 1;
771 if (!(m->flags & ACC_STATIC)) {
772 fprintf(file, ", struct ");
773 printID(m->class->name);
774 fprintf(file, "* this");
777 fprintf(file, ", jclass clazz");
780 if ((*utf_ptr) != ')') fprintf(file, ", ");
782 while ((*utf_ptr) != ')') {
783 utf_ptr = printtype(utf_ptr);
784 fprintf(file, " par%d", paramnum++);
785 if ((*utf_ptr)!=')') fprintf(file, ", ");
788 fprintf(file, ");\n\n");
792 /******* remove package-name in fully-qualified classname *********************/
794 void gen_header_filename(char *buffer, utf *u)
798 for (i = 0; i < utf_get_number_of_u2s(u); i++) {
799 if ((u->text[i] == '/') || (u->text[i] == '$')) {
800 buffer[i] = '_'; /* convert '$' and '/' to '_' */
803 buffer[i] = u->text[i];
806 buffer[utf_get_number_of_u2s(u)] = '\0';
810 /* create headerfile for classes and store native methods in chain ************/
812 void headerfile_generate(classinfo *c, char *opt_directory)
814 char header_filename[1024] = "";
815 char classname[1024];
816 char uclassname[1024];
821 bool nativelyoverloaded;
823 /* prevent compiler warnings */
825 nativelyoverloaded = false;
827 /* open headerfile for class */
828 gen_header_filename(classname, c->name);
830 /* create chain for renaming fields */
831 ident_chain = chain_new();
834 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
837 sprintf(header_filename, "%s.h", classname);
840 file = fopen(header_filename, "w");
842 log_text("Can not open file to store header information");
846 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
848 /* convert to uppercase */
849 for (i = 0; classname[i]; i++) {
850 uclassname[i] = toupper(classname[i]);
852 uclassname[i] = '\0';
854 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
856 /* create structure for direct access to objects */
857 fprintf(file, "/* Structure information for class: ");
858 utf_fprint_printable_ascii(file, c->name);
859 fprintf(file, " */\n\n");
860 fprintf(file, "typedef struct ");
862 fprintf(file, " {\n");
870 fprintf(file, ";\n\n");
872 /* create chain for renaming overloaded methods */
873 chain_free(ident_chain);
874 ident_chain = chain_new();
876 /* create method-prototypes */
878 /* find overloaded methods */
880 for (i = 0; i < c->methodscount; i++) {
881 m = &(c->methods[i]);
883 if (!(m->flags & ACC_NATIVE))
886 /* We use a dummy flag here. */
888 if (!(m->flags & ACC_NATIVELY_OVERLOADED)) {
889 nativelyoverloaded = false;
891 for (j = i + 1; j < c->methodscount; j++) {
892 m2 = &(c->methods[j]);
894 if (!(m2->flags & ACC_NATIVE))
897 if (m->name == m2->name) {
898 m2->flags |= ACC_NATIVELY_OVERLOADED;
899 nativelyoverloaded = true;
904 if (nativelyoverloaded == true)
905 m->flags |= ACC_NATIVELY_OVERLOADED;
908 for (i = 0; i < c->methodscount; i++) {
909 m = &(c->methods[i]);
911 if (m->flags & ACC_NATIVE)
915 chain_free(ident_chain);
917 fprintf(file, "#endif\n\n");
923 /******** print classname, '$' used to seperate inner-class name ***********/
925 void print_classname(classinfo *clazz)
927 utf *u = clazz->name;
928 char *endpos = u->text + u->blength;
929 char *utf_ptr = u->text;
932 while (utf_ptr < endpos) {
933 if ((c = utf_nextu2(&utf_ptr)) == '_')
940 /* jvmti releated functions ************************************************/
942 #if defined(ENABLE_JVMTI)
943 void jvmti_ThreadStartEnd(int ev) {;}
944 void jvmti_ClassLoadPrepare(bool prepared, classinfo *c) {;}
945 void jvmti_MonitorContendedEntering(bool entered, jobject obj) {;}
951 * These are local overrides for various environment variables in Emacs.
952 * Please do not remove this and leave it at the end of the file, where
953 * Emacs will automagically detect them.
954 * ---------------------------------------------------------------------
957 * indent-tabs-mode: t
961 * vim:noexpandtab:sw=4:ts=4: