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 5814 2006-10-20 14:53:27Z 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) {}
147 #if defined(ENABLE_JIT)
148 void asm_abstractmethoderror(void) {}
149 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
152 #if defined(ENABLE_INTRP)
153 void intrp_asm_abstractmethoderror(void) {}
154 void intrp_asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
157 u8 asm_get_cycle_count(void)
163 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
168 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
170 return typecheck_TRUE;
173 void typeinfo_init_classinfo(typeinfo *info,classinfo *c)
177 bool typeinfo_init_class(typeinfo *info,classref_or_classinfo c)
182 void typeinfo_print(FILE *file,typeinfo *info,int indent) {}
183 void typeinfo_print_short(FILE *file,typeinfo *info) {}
185 void exceptions_print_exception(java_objectheader *xptr) {}
186 void stacktrace_dump_trace(threadobject *thread) {}
187 void stacktrace_print_trace(java_objectheader *xptr) {}
188 java_objectarray *stacktrace_getClassContext() { return NULL; }
189 void code_free_code_of_method(methodinfo *m) {}
192 /* exception functions ********************************************************/
194 /* these should not be called */
196 void throw_main_exception_exit(void) { assert(0); }
197 void throw_exception(void) { assert(0); }
198 void throw_exception_exit(void) { assert(0); }
200 void exceptions_throw_verifyerror(methodinfo *m, const char *message)
205 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
209 /* keep compiler happy */
215 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
219 fprintf(stderr, "%s: ", exception);
221 va_start(ap, message);
222 vfprintf(stderr, message, ap);
231 void exceptions_throw_outofmemory_exit(void)
233 fprintf(stderr, "java.lang.InternalError: Out of memory\n");
238 java_objectheader *new_exception(const char *classname)
240 fprintf(stderr, "%s\n", classname);
243 /* keep compiler happy */
249 java_objectheader *new_exception_message(const char *classname, const char *message)
251 fprintf(stderr, "%s: %s\n", classname, message);
254 /* keep compiler happy */
260 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
262 fprintf(stderr, "%s: ", classname);
263 utf_display_printable_ascii(message);
268 /* keep compiler happy */
274 java_objectheader *new_exception_javastring(const char *classname,
275 java_lang_String *message)
277 fprintf(stderr, "%s: ", classname);
278 /* TODO print message */
283 /* keep compiler happy */
289 void exceptions_throw_abstractmethoderror(void)
291 fprintf(stderr, "java.lang.AbstractMethodError\n");
297 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
301 utf_display_printable_ascii(c->name);
302 fprintf(stderr, ": ");
304 va_start(ap, message);
305 vfprintf(stderr, message, ap);
312 /* keep compiler happy */
318 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
322 va_start(ap, message);
323 (void) new_classformaterror(c, message, ap);
328 java_objectheader *new_classnotfoundexception(utf *name)
330 fprintf(stderr, "java.lang.ClassNotFoundException: ");
331 utf_fprint_printable_ascii(stderr, name);
336 /* keep compiler happy */
342 java_objectheader *new_noclassdeffounderror(utf *name)
344 fprintf(stderr, "java.lang.NoClassDefFoundError: ");
345 utf_fprint_printable_ascii(stderr, name);
350 /* keep compiler happy */
356 java_objectheader *exceptions_new_linkageerror(const char *message,
359 fprintf(stderr, "java.lang.LinkageError: %s",message);
361 utf_fprint_printable_ascii_classname(stderr, c->name);
367 /* keep compiler happy */
372 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
373 utf *name, utf *desc)
375 fprintf(stderr, "java.lang.NoSuchMethodError: ");
376 utf_fprint_printable_ascii(stderr, c->name);
377 fprintf(stderr, ".");
378 utf_fprint_printable_ascii(stderr, name);
379 utf_fprint_printable_ascii(stderr, desc);
384 /* keep compiler happy */
390 void exceptions_throw_nosuchmethoderror(classinfo *c, utf *name, utf *desc)
392 (void) exceptions_new_nosuchmethoderror(c, name, desc);
396 java_objectheader *new_internalerror(const char *message, ...)
400 fprintf(stderr, "%s: ", string_java_lang_InternalError);
402 va_start(ap, message);
403 vfprintf(stderr, message, ap);
408 /* keep compiler happy */
414 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
418 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
420 utf_display_printable_ascii(c->name);
421 fprintf(stderr, ": ");
423 va_start(ap, message);
424 vfprintf(stderr, message, ap);
429 /* keep compiler happy */
435 void exceptions_throw_arrayindexoutofboundsexception(void)
437 fprintf(stderr, "%s", string_java_lang_ArrayIndexOutOfBoundsException);
442 java_objectheader *exceptions_new_arraystoreexception(void)
444 fprintf(stderr, "%s", string_java_lang_ArrayStoreException);
447 /* keep compiler happy */
453 void exceptions_throw_arraystoreexception(void)
455 (void) exceptions_new_arraystoreexception();
459 java_objectheader *exceptions_throw_illegalmonitorstateexception(void)
461 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
464 /* keep compiler happy */
470 java_objectheader *new_negativearraysizeexception(void)
472 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
475 /* keep compiler happy */
481 void exceptions_throw_negativearraysizeexception(void)
483 (void) new_negativearraysizeexception();
487 java_objectheader *new_nullpointerexception(void)
489 fprintf(stderr, "%s", string_java_lang_NullPointerException);
492 /* keep compiler happy */
498 void exceptions_throw_nullpointerexception(void)
500 (void) new_nullpointerexception();
504 void classnotfoundexception_to_noclassdeffounderror(void)
508 /* machine dependent stuff ****************************************************/
510 #if defined(ENABLE_THREADS)
511 critical_section_node_t asm_criticalsections;
512 void thread_restartcriticalsection(ucontext_t *uc) {}
515 void md_param_alloc(methoddesc *md) {}
518 #if defined(ENABLE_INTRP)
519 void print_dynamic_super_statistics(void) {}
523 /************************ global variables **********************/
525 #define ACC_NATIVELY_OVERLOADED 0x10000000
527 chain *ident_chain; /* chain with method and field names in current class */
529 static u4 outputsize;
530 static bool dopadding;
533 static void printIDpart(int c)
535 if ((c >= 'a' && c <= 'z') ||
536 (c >= 'A' && c <= 'Z') ||
537 (c >= '0' && c <= '9') ||
547 char *utf_ptr = u->text;
550 for (i = 0; i < utf_get_number_of_u2s(u); i++)
551 printIDpart(utf_nextu2(&utf_ptr));
555 static void addoutputsize (int len)
558 if (!dopadding) return;
560 newsize = ALIGN(outputsize, len);
562 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
563 outputsize = newsize;
567 void printOverloadPart(utf *desc)
569 char *utf_ptr=desc->text;
574 while ((c = utf_nextu2(&utf_ptr)) != ')') {
584 fprintf(file, "%c", (char) c);
591 while ((c = utf_nextu2(&utf_ptr)) != ';')
598 log_text("invalid method descriptor");
604 static char *printtype(char *utf_ptr)
608 switch (utf_nextu2(&utf_ptr)) {
609 case 'V': fprintf (file, "void");
615 case 'Z': addoutputsize (4);
616 fprintf (file, "s4");
618 case 'J': addoutputsize (8);
619 fprintf (file, "s8");
621 case 'F': addoutputsize (4);
622 fprintf (file, "float");
624 case 'D': addoutputsize (8);
625 fprintf (file, "double");
628 addoutputsize ( sizeof(java_arrayheader*) );
629 switch (utf_nextu2(&utf_ptr)) {
630 case 'I': fprintf (file, "java_intarray*"); break;
631 case 'J': fprintf (file, "java_longarray*"); break;
632 case 'Z': fprintf (file, "java_booleanarray*"); break;
633 case 'B': fprintf (file, "java_bytearray*"); break;
634 case 'S': fprintf (file, "java_shortarray*"); break;
635 case 'C': fprintf (file, "java_chararray*"); break;
636 case 'F': fprintf (file, "java_floatarray*"); break;
637 case 'D': fprintf (file, "java_doublearray*"); break;
639 case '[': fprintf(file, "java_objectarray*");
640 while ((c = utf_nextu2(&utf_ptr)) == '[');
642 while (utf_nextu2(&utf_ptr) != ';');
645 case 'L': fprintf(file, "java_objectarray*");
646 while (utf_nextu2(&utf_ptr) != ';');
649 log_text("invalid type descriptor");
655 addoutputsize ( sizeof(java_objectheader*));
656 fprintf (file, "struct ");
657 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
662 log_text("Unknown type in field descriptor");
670 /***** determine the number of entries of a utf string in the ident chain *****/
672 static int searchidentchain_utf(utf *ident)
674 utf *u = chain_first(ident_chain); /* first element of list */
678 if (u==ident) count++; /* string found */
679 u = chain_next(ident_chain); /* next element in list */
686 /************** print structure for direct access to objects ******************/
688 static void printfields(classinfo *c)
695 addoutputsize(sizeof(java_objectheader));
696 fprintf(file, " java_objectheader header;\n");
700 printfields(c->super.cls);
702 for (i = 0; i < c->fieldscount; i++) {
705 if (!(f->flags & ACC_STATIC)) {
707 printtype(f->descriptor->text);
709 utf_fprint_printable_ascii(file, f->name);
711 /* rename multiple fieldnames */
712 if ((ident_count = searchidentchain_utf(f->name)))
713 fprintf(file, "%d", ident_count - 1);
714 chain_addlast(ident_chain, f->name);
716 fprintf(file, ";\n");
722 /***************** store prototype for native method in file ******************/
724 void printmethod(methodinfo *m)
729 /* search for return-type in descriptor */
730 utf_ptr = m->descriptor->text;
731 while (utf_nextu2(&utf_ptr) != ')');
734 fprintf(file, "\n/*\n * Class: ");
735 utf_fprint_printable_ascii(file, m->class->name);
736 fprintf(file, "\n * Method: ");
737 utf_fprint_printable_ascii(file, m->name);
738 fprintf(file, "\n * Signature: ");
739 utf_fprint_printable_ascii(file, m->descriptor);
740 fprintf(file, "\n */\n");
742 /* create prototype */
743 fprintf(file, "JNIEXPORT ");
745 fprintf(file, " JNICALL Java_");
746 printID(m->class->name);
748 chain_addlast(ident_chain, m->name);
753 /* ATTENTION: We use a dummy flag here. */
755 if (m->flags & ACC_NATIVELY_OVERLOADED)
756 printOverloadPart(m->descriptor);
758 fprintf(file, "(JNIEnv *env");
760 utf_ptr = m->descriptor->text + 1;
762 if (!(m->flags & ACC_STATIC)) {
763 fprintf(file, ", struct ");
764 printID(m->class->name);
765 fprintf(file, "* this");
768 fprintf(file, ", jclass clazz");
771 if ((*utf_ptr) != ')') fprintf(file, ", ");
773 while ((*utf_ptr) != ')') {
774 utf_ptr = printtype(utf_ptr);
775 fprintf(file, " par%d", paramnum++);
776 if ((*utf_ptr)!=')') fprintf(file, ", ");
779 fprintf(file, ");\n\n");
783 /******* remove package-name in fully-qualified classname *********************/
785 void gen_header_filename(char *buffer, utf *u)
789 for (i = 0; i < utf_get_number_of_u2s(u); i++) {
790 if ((u->text[i] == '/') || (u->text[i] == '$')) {
791 buffer[i] = '_'; /* convert '$' and '/' to '_' */
794 buffer[i] = u->text[i];
797 buffer[utf_get_number_of_u2s(u)] = '\0';
801 /* create headerfile for classes and store native methods in chain ************/
803 void headerfile_generate(classinfo *c, char *opt_directory)
805 char header_filename[1024] = "";
806 char classname[1024];
807 char uclassname[1024];
812 bool nativelyoverloaded;
814 /* prevent compiler warnings */
816 nativelyoverloaded = false;
818 /* open headerfile for class */
819 gen_header_filename(classname, c->name);
821 /* create chain for renaming fields */
822 ident_chain = chain_new();
825 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
828 sprintf(header_filename, "%s.h", classname);
831 file = fopen(header_filename, "w");
833 log_text("Can not open file to store header information");
837 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
839 /* convert to uppercase */
840 for (i = 0; classname[i]; i++) {
841 uclassname[i] = toupper(classname[i]);
843 uclassname[i] = '\0';
845 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
847 /* create structure for direct access to objects */
848 fprintf(file, "/* Structure information for class: ");
849 utf_fprint_printable_ascii(file, c->name);
850 fprintf(file, " */\n\n");
851 fprintf(file, "typedef struct ");
853 fprintf(file, " {\n");
861 fprintf(file, ";\n\n");
863 /* create chain for renaming overloaded methods */
864 chain_free(ident_chain);
865 ident_chain = chain_new();
867 /* create method-prototypes */
869 /* find overloaded methods */
871 for (i = 0; i < c->methodscount; i++) {
872 m = &(c->methods[i]);
874 if (!(m->flags & ACC_NATIVE))
877 /* We use a dummy flag here. */
879 if (!(m->flags & ACC_NATIVELY_OVERLOADED)) {
880 nativelyoverloaded = false;
882 for (j = i + 1; j < c->methodscount; j++) {
883 m2 = &(c->methods[j]);
885 if (!(m2->flags & ACC_NATIVE))
888 if (m->name == m2->name) {
889 m2->flags |= ACC_NATIVELY_OVERLOADED;
890 nativelyoverloaded = true;
895 if (nativelyoverloaded == true)
896 m->flags |= ACC_NATIVELY_OVERLOADED;
899 for (i = 0; i < c->methodscount; i++) {
900 m = &(c->methods[i]);
902 if (m->flags & ACC_NATIVE)
906 chain_free(ident_chain);
908 fprintf(file, "#endif\n\n");
914 /******** print classname, '$' used to seperate inner-class name ***********/
916 void print_classname(classinfo *clazz)
918 utf *u = clazz->name;
919 char *endpos = u->text + u->blength;
920 char *utf_ptr = u->text;
923 while (utf_ptr < endpos) {
924 if ((c = utf_nextu2(&utf_ptr)) == '_')
931 /* jvmti releated functions ************************************************/
933 #if defined(ENABLE_JVMTI)
934 void jvmti_ThreadStartEnd(int ev) {;}
935 void jvmti_ClassLoadPrepare(bool prepared, classinfo *c) {;}
936 void jvmti_MonitorContendedEntering(bool entered, jobject obj) {;}
942 * These are local overrides for various environment variables in Emacs.
943 * Please do not remove this and leave it at the end of the file, where
944 * Emacs will automagically detect them.
945 * ---------------------------------------------------------------------
948 * indent-tabs-mode: t
952 * vim:noexpandtab:sw=4:ts=4: