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 5900 2006-11-04 17:30:44Z michi $
49 #if defined(ENABLE_THREADS)
50 # if defined(__DARWIN__)
53 # include <ucontext.h>
56 #include "mm/gc-common.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, ...)
114 /* print the log message */
117 vfprintf(stderr, text, ap);
120 /* now abort the VM */
125 /* code patching functions */
126 void patcher_builtin_arraycheckcast(u1 *sp) {}
128 #if defined(__MIPS__)
129 long compare_and_swap(long *p, long oldval, long newval)
140 u1 *createcompilerstub(methodinfo *m) { return NULL; }
141 #if defined(ENABLE_INTRP)
142 u1 *intrp_createcompilerstub(methodinfo *m) { return NULL; }
145 codeinfo *codegen_createnativestub(functionptr f, methodinfo *m) { return NULL; }
147 void removecompilerstub(u1 *stub) {}
148 void removenativestub(u1 *stub) {}
150 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
151 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
153 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
155 void asm_handle_builtin_exception(classinfo *c) {}
157 #if defined(ENABLE_JIT)
158 void asm_abstractmethoderror(void) {}
159 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
162 #if defined(ENABLE_INTRP)
163 void intrp_asm_abstractmethoderror(void) {}
164 void intrp_asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
167 u8 asm_get_cycle_count(void)
173 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
178 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
180 return typecheck_TRUE;
183 void typeinfo_init_classinfo(typeinfo *info,classinfo *c)
187 bool typeinfo_init_class(typeinfo *info,classref_or_classinfo c)
192 void typeinfo_print(FILE *file,typeinfo *info,int indent) {}
193 void typeinfo_print_short(FILE *file,typeinfo *info) {}
195 void exceptions_print_exception(java_objectheader *xptr) {}
196 void stacktrace_dump_trace(threadobject *thread) {}
197 void stacktrace_print_trace(java_objectheader *xptr) {}
198 java_objectarray *stacktrace_getClassContext() { return NULL; }
199 void code_free_code_of_method(methodinfo *m) {}
202 /* exception functions ********************************************************/
204 /* these should not be called */
206 void throw_main_exception_exit(void) { assert(0); }
207 void throw_exception(void) { assert(0); }
208 void throw_exception_exit(void) { assert(0); }
210 void exceptions_throw_verifyerror(methodinfo *m, const char *message)
215 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
219 /* keep compiler happy */
225 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
229 fprintf(stderr, "%s: ", exception);
231 va_start(ap, message);
232 vfprintf(stderr, message, ap);
241 void exceptions_throw_outofmemory_exit(void)
243 fprintf(stderr, "java.lang.InternalError: Out of memory\n");
248 java_objectheader *new_exception(const char *classname)
250 fprintf(stderr, "%s\n", classname);
253 /* keep compiler happy */
259 java_objectheader *new_exception_message(const char *classname, const char *message)
261 fprintf(stderr, "%s: %s\n", classname, message);
264 /* keep compiler happy */
270 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
272 fprintf(stderr, "%s: ", classname);
273 utf_display_printable_ascii(message);
278 /* keep compiler happy */
284 java_objectheader *new_exception_javastring(const char *classname,
285 java_lang_String *message)
287 fprintf(stderr, "%s: ", classname);
288 /* TODO print message */
293 /* keep compiler happy */
299 void exceptions_throw_abstractmethoderror(void)
301 fprintf(stderr, "java.lang.AbstractMethodError\n");
307 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
311 utf_display_printable_ascii(c->name);
312 fprintf(stderr, ": ");
314 va_start(ap, message);
315 vfprintf(stderr, message, ap);
322 /* keep compiler happy */
328 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
332 va_start(ap, message);
333 (void) new_classformaterror(c, message, ap);
338 java_objectheader *new_classnotfoundexception(utf *name)
340 fprintf(stderr, "java.lang.ClassNotFoundException: ");
341 utf_fprint_printable_ascii(stderr, name);
346 /* keep compiler happy */
352 java_objectheader *new_noclassdeffounderror(utf *name)
354 fprintf(stderr, "java.lang.NoClassDefFoundError: ");
355 utf_fprint_printable_ascii(stderr, name);
360 /* keep compiler happy */
366 java_objectheader *exceptions_new_linkageerror(const char *message,
369 fprintf(stderr, "java.lang.LinkageError: %s",message);
371 utf_fprint_printable_ascii_classname(stderr, c->name);
377 /* keep compiler happy */
382 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
383 utf *name, utf *desc)
385 fprintf(stderr, "java.lang.NoSuchMethodError: ");
386 utf_fprint_printable_ascii(stderr, c->name);
387 fprintf(stderr, ".");
388 utf_fprint_printable_ascii(stderr, name);
389 utf_fprint_printable_ascii(stderr, desc);
394 /* keep compiler happy */
400 void exceptions_throw_nosuchmethoderror(classinfo *c, utf *name, utf *desc)
402 (void) exceptions_new_nosuchmethoderror(c, name, desc);
406 java_objectheader *new_internalerror(const char *message, ...)
410 fprintf(stderr, "%s: ", string_java_lang_InternalError);
412 va_start(ap, message);
413 vfprintf(stderr, message, ap);
418 /* keep compiler happy */
424 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
428 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
430 utf_display_printable_ascii(c->name);
431 fprintf(stderr, ": ");
433 va_start(ap, message);
434 vfprintf(stderr, message, ap);
439 /* keep compiler happy */
445 void exceptions_throw_arrayindexoutofboundsexception(void)
447 fprintf(stderr, "%s", string_java_lang_ArrayIndexOutOfBoundsException);
452 java_objectheader *exceptions_new_arraystoreexception(void)
454 fprintf(stderr, "%s", string_java_lang_ArrayStoreException);
457 /* keep compiler happy */
463 void exceptions_throw_arraystoreexception(void)
465 (void) exceptions_new_arraystoreexception();
469 java_objectheader *exceptions_throw_illegalmonitorstateexception(void)
471 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
474 /* keep compiler happy */
480 java_objectheader *new_negativearraysizeexception(void)
482 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
485 /* keep compiler happy */
491 void exceptions_throw_negativearraysizeexception(void)
493 (void) new_negativearraysizeexception();
497 java_objectheader *new_nullpointerexception(void)
499 fprintf(stderr, "%s", string_java_lang_NullPointerException);
502 /* keep compiler happy */
508 void exceptions_throw_nullpointerexception(void)
510 (void) new_nullpointerexception();
514 void classnotfoundexception_to_noclassdeffounderror(void)
518 /* machine dependent stuff ****************************************************/
520 #if defined(ENABLE_THREADS)
521 critical_section_node_t asm_criticalsections;
522 void thread_restartcriticalsection(ucontext_t *uc) {}
525 void md_param_alloc(methoddesc *md) {}
528 #if defined(ENABLE_INTRP)
529 void print_dynamic_super_statistics(void) {}
533 /************************ global variables **********************/
535 #define ACC_NATIVELY_OVERLOADED 0x10000000
537 chain *ident_chain; /* chain with method and field names in current class */
539 static u4 outputsize;
540 static bool dopadding;
543 static void printIDpart(int c)
545 if ((c >= 'a' && c <= 'z') ||
546 (c >= 'A' && c <= 'Z') ||
547 (c >= '0' && c <= '9') ||
557 char *utf_ptr = u->text;
560 for (i = 0; i < utf_get_number_of_u2s(u); i++)
561 printIDpart(utf_nextu2(&utf_ptr));
565 static void addoutputsize (int len)
568 if (!dopadding) return;
570 newsize = MEMORY_ALIGN(outputsize, len);
572 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
573 outputsize = newsize;
577 void printOverloadPart(utf *desc)
579 char *utf_ptr=desc->text;
584 while ((c = utf_nextu2(&utf_ptr)) != ')') {
594 fprintf(file, "%c", (char) c);
601 while ((c = utf_nextu2(&utf_ptr)) != ';')
608 log_text("invalid method descriptor");
614 static char *printtype(char *utf_ptr)
618 switch (utf_nextu2(&utf_ptr)) {
619 case 'V': fprintf (file, "void");
625 case 'Z': addoutputsize (4);
626 fprintf (file, "s4");
628 case 'J': addoutputsize (8);
629 fprintf (file, "s8");
631 case 'F': addoutputsize (4);
632 fprintf (file, "float");
634 case 'D': addoutputsize (8);
635 fprintf (file, "double");
638 addoutputsize ( sizeof(java_arrayheader*) );
639 switch (utf_nextu2(&utf_ptr)) {
640 case 'I': fprintf (file, "java_intarray*"); break;
641 case 'J': fprintf (file, "java_longarray*"); break;
642 case 'Z': fprintf (file, "java_booleanarray*"); break;
643 case 'B': fprintf (file, "java_bytearray*"); break;
644 case 'S': fprintf (file, "java_shortarray*"); break;
645 case 'C': fprintf (file, "java_chararray*"); break;
646 case 'F': fprintf (file, "java_floatarray*"); break;
647 case 'D': fprintf (file, "java_doublearray*"); break;
649 case '[': fprintf(file, "java_objectarray*");
650 while ((c = utf_nextu2(&utf_ptr)) == '[');
652 while (utf_nextu2(&utf_ptr) != ';');
655 case 'L': fprintf(file, "java_objectarray*");
656 while (utf_nextu2(&utf_ptr) != ';');
659 log_text("invalid type descriptor");
665 addoutputsize ( sizeof(java_objectheader*));
666 fprintf (file, "struct ");
667 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
672 log_text("Unknown type in field descriptor");
680 /***** determine the number of entries of a utf string in the ident chain *****/
682 static int searchidentchain_utf(utf *ident)
684 utf *u = chain_first(ident_chain); /* first element of list */
688 if (u==ident) count++; /* string found */
689 u = chain_next(ident_chain); /* next element in list */
696 /************** print structure for direct access to objects ******************/
698 static void printfields(classinfo *c)
705 addoutputsize(sizeof(java_objectheader));
706 fprintf(file, " java_objectheader header;\n");
710 printfields(c->super.cls);
712 for (i = 0; i < c->fieldscount; i++) {
715 if (!(f->flags & ACC_STATIC)) {
717 printtype(f->descriptor->text);
719 utf_fprint_printable_ascii(file, f->name);
721 /* rename multiple fieldnames */
722 if ((ident_count = searchidentchain_utf(f->name)))
723 fprintf(file, "%d", ident_count - 1);
724 chain_addlast(ident_chain, f->name);
726 fprintf(file, ";\n");
732 /***************** store prototype for native method in file ******************/
734 void printmethod(methodinfo *m)
739 /* search for return-type in descriptor */
740 utf_ptr = m->descriptor->text;
741 while (utf_nextu2(&utf_ptr) != ')');
744 fprintf(file, "\n/*\n * Class: ");
745 utf_fprint_printable_ascii(file, m->class->name);
746 fprintf(file, "\n * Method: ");
747 utf_fprint_printable_ascii(file, m->name);
748 fprintf(file, "\n * Signature: ");
749 utf_fprint_printable_ascii(file, m->descriptor);
750 fprintf(file, "\n */\n");
752 /* create prototype */
753 fprintf(file, "JNIEXPORT ");
755 fprintf(file, " JNICALL Java_");
756 printID(m->class->name);
758 chain_addlast(ident_chain, m->name);
763 /* ATTENTION: We use a dummy flag here. */
765 if (m->flags & ACC_NATIVELY_OVERLOADED)
766 printOverloadPart(m->descriptor);
768 fprintf(file, "(JNIEnv *env");
770 utf_ptr = m->descriptor->text + 1;
772 if (!(m->flags & ACC_STATIC)) {
773 fprintf(file, ", struct ");
774 printID(m->class->name);
775 fprintf(file, "* this");
778 fprintf(file, ", jclass clazz");
781 if ((*utf_ptr) != ')') fprintf(file, ", ");
783 while ((*utf_ptr) != ')') {
784 utf_ptr = printtype(utf_ptr);
785 fprintf(file, " par%d", paramnum++);
786 if ((*utf_ptr)!=')') fprintf(file, ", ");
789 fprintf(file, ");\n\n");
793 /******* remove package-name in fully-qualified classname *********************/
795 void gen_header_filename(char *buffer, utf *u)
799 for (i = 0; i < utf_get_number_of_u2s(u); i++) {
800 if ((u->text[i] == '/') || (u->text[i] == '$')) {
801 buffer[i] = '_'; /* convert '$' and '/' to '_' */
804 buffer[i] = u->text[i];
807 buffer[utf_get_number_of_u2s(u)] = '\0';
811 /* create headerfile for classes and store native methods in chain ************/
813 void headerfile_generate(classinfo *c, char *opt_directory)
815 char header_filename[1024] = "";
816 char classname[1024];
817 char uclassname[1024];
822 bool nativelyoverloaded;
824 /* prevent compiler warnings */
826 nativelyoverloaded = false;
828 /* open headerfile for class */
829 gen_header_filename(classname, c->name);
831 /* create chain for renaming fields */
832 ident_chain = chain_new();
835 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
838 sprintf(header_filename, "%s.h", classname);
841 file = fopen(header_filename, "w");
843 log_text("Can not open file to store header information");
847 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
849 /* convert to uppercase */
850 for (i = 0; classname[i]; i++) {
851 uclassname[i] = toupper(classname[i]);
853 uclassname[i] = '\0';
855 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
857 /* create structure for direct access to objects */
858 fprintf(file, "/* Structure information for class: ");
859 utf_fprint_printable_ascii(file, c->name);
860 fprintf(file, " */\n\n");
861 fprintf(file, "typedef struct ");
863 fprintf(file, " {\n");
871 fprintf(file, ";\n\n");
873 /* create chain for renaming overloaded methods */
874 chain_free(ident_chain);
875 ident_chain = chain_new();
877 /* create method-prototypes */
879 /* find overloaded methods */
881 for (i = 0; i < c->methodscount; i++) {
882 m = &(c->methods[i]);
884 if (!(m->flags & ACC_NATIVE))
887 /* We use a dummy flag here. */
889 if (!(m->flags & ACC_NATIVELY_OVERLOADED)) {
890 nativelyoverloaded = false;
892 for (j = i + 1; j < c->methodscount; j++) {
893 m2 = &(c->methods[j]);
895 if (!(m2->flags & ACC_NATIVE))
898 if (m->name == m2->name) {
899 m2->flags |= ACC_NATIVELY_OVERLOADED;
900 nativelyoverloaded = true;
905 if (nativelyoverloaded == true)
906 m->flags |= ACC_NATIVELY_OVERLOADED;
909 for (i = 0; i < c->methodscount; i++) {
910 m = &(c->methods[i]);
912 if (m->flags & ACC_NATIVE)
916 chain_free(ident_chain);
918 fprintf(file, "#endif\n\n");
924 /******** print classname, '$' used to seperate inner-class name ***********/
926 void print_classname(classinfo *clazz)
928 utf *u = clazz->name;
929 char *endpos = u->text + u->blength;
930 char *utf_ptr = u->text;
933 while (utf_ptr < endpos) {
934 if ((c = utf_nextu2(&utf_ptr)) == '_')
941 /* jvmti releated functions ************************************************/
943 #if defined(ENABLE_JVMTI)
944 void jvmti_ThreadStartEnd(int ev) {;}
945 void jvmti_ClassLoadPrepare(bool prepared, classinfo *c) {;}
946 void jvmti_MonitorContendedEntering(bool entered, jobject obj) {;}
952 * These are local overrides for various environment variables in Emacs.
953 * Please do not remove this and leave it at the end of the file, where
954 * Emacs will automagically detect them.
955 * ---------------------------------------------------------------------
958 * indent-tabs-mode: t
962 * vim:noexpandtab:sw=4:ts=4: