1 /* src/cacaoh/headers.c - functions for header generation
3 Copyright (C) 1996-2005, 2006, 2007 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 $Id: headers.c 7228 2007-01-19 01:13:48Z edwin $
40 #if defined(ENABLE_THREADS)
41 # if defined(__DARWIN__)
44 # include <ucontext.h>
47 #include "mm/gc-common.h"
48 #include "mm/memory.h"
49 #include "native/include/java_lang_String.h"
50 #include "native/include/java_lang_Throwable.h"
51 #include "toolbox/chain.h"
52 #include "toolbox/logging.h"
53 #include "vm/builtin.h"
55 #include "vm/global.h"
56 #include "vm/method.h"
57 #include "vm/loader.h"
58 #include "vm/options.h"
59 #include "vm/stringlocal.h"
60 #include "vm/jit/asmpart.h"
63 /* Invocation API variables ***************************************************/
65 _Jv_JavaVM *_Jv_jvm; /* denotes a Java VM */
66 char *_Jv_bootclasspath;
69 char *classpath_libdir;
72 char *_Jv_java_library_path;
74 #if defined(ENABLE_INTRP)
75 /* dummy interpreter stack to keep the compiler happy */
81 /* for raising exceptions from native methods *********************************/
83 #if !defined(ENABLE_THREADS)
84 java_objectheader *_no_threads_exceptionptr = NULL;
88 /* replace some non-vmcore functions ******************************************/
90 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
93 /* return something different than NULL, otherwise we get an exception */
95 return (functionptr) 1;
98 java_objectheader *native_new_and_init(classinfo *c) { return NULL; }
99 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s) { return NULL; }
100 java_objectheader *native_new_and_init_int(classinfo *c, s4 i) { return NULL; }
101 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t) { return NULL; }
104 java_objectheader *vm_call_method(methodinfo *m, java_objectheader *o, ...)
107 void vm_abort(const char *text, ...)
111 /* print the log message */
114 vfprintf(stderr, text, ap);
117 /* now abort the VM */
122 /* code patching functions */
123 void patcher_builtin_arraycheckcast(u1 *sp) {}
126 long compare_and_swap(volatile long *p, long oldval, long newval)
138 long asm_compare_and_swap(volatile long *p, long oldval, long newval)
150 void asm_memory_barrier(void)
155 u1 *createcompilerstub(methodinfo *m) { return NULL; }
156 #if defined(ENABLE_INTRP)
157 u1 *intrp_createcompilerstub(methodinfo *m) { return NULL; }
160 codeinfo *codegen_createnativestub(functionptr f, methodinfo *m) { return NULL; }
162 void removecompilerstub(u1 *stub) {}
163 void removenativestub(u1 *stub) {}
165 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
166 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
168 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
170 void asm_handle_builtin_exception(classinfo *c) {}
172 #if defined(ENABLE_JIT)
173 void asm_abstractmethoderror(void) {}
174 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
177 #if defined(ENABLE_INTRP)
178 void intrp_asm_abstractmethoderror(void) {}
179 void intrp_asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
182 u8 asm_get_cycle_count(void)
188 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
193 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
195 return typecheck_TRUE;
198 void typeinfo_init_classinfo(typeinfo *info,classinfo *c)
202 bool typeinfo_init_class(typeinfo *info,classref_or_classinfo c)
207 void typeinfo_print(FILE *file,typeinfo *info,int indent) {}
208 void typeinfo_print_short(FILE *file,typeinfo *info) {}
210 void exceptions_print_exception(java_objectheader *xptr) {}
211 void stacktrace_dump_trace(threadobject *thread) {}
212 void stacktrace_print_trace(java_objectheader *xptr) {}
213 java_objectarray *stacktrace_getClassContext() { return NULL; }
214 void code_free_code_of_method(methodinfo *m) {}
216 void jit_invalidate_code(methodinfo *m) {}
219 /* exception functions ********************************************************/
221 /* these should not be called */
223 void throw_main_exception_exit(void) { assert(0); }
224 void throw_exception(void) { assert(0); }
225 void throw_exception_exit(void) { assert(0); }
227 void exceptions_throw_verifyerror(methodinfo *m, const char *message)
232 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
236 /* keep compiler happy */
242 java_objectheader *new_exception(const char *classname)
244 fprintf(stderr, "%s\n", classname);
247 /* keep compiler happy */
253 java_objectheader *new_exception_message(const char *classname, const char *message)
255 fprintf(stderr, "%s: %s\n", classname, message);
258 /* keep compiler happy */
264 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
266 fprintf(stderr, "%s: ", classname);
267 utf_display_printable_ascii(message);
272 /* keep compiler happy */
278 java_objectheader *new_exception_javastring(const char *classname,
279 java_lang_String *message)
281 fprintf(stderr, "%s: ", classname);
282 /* TODO print message */
287 /* keep compiler happy */
293 void exceptions_throw_abstractmethoderror(void)
295 fprintf(stderr, "java.lang.AbstractMethodError\n");
301 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
305 utf_display_printable_ascii(c->name);
306 fprintf(stderr, ": ");
308 va_start(ap, message);
309 vfprintf(stderr, message, ap);
316 /* keep compiler happy */
322 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
326 va_start(ap, message);
327 (void) new_classformaterror(c, message, ap);
332 java_objectheader *new_classnotfoundexception(utf *name)
334 fprintf(stderr, "java.lang.ClassNotFoundException: ");
335 utf_fprint_printable_ascii(stderr, name);
340 /* keep compiler happy */
346 java_objectheader *new_noclassdeffounderror(utf *name)
348 fprintf(stderr, "java.lang.NoClassDefFoundError: ");
349 utf_fprint_printable_ascii(stderr, name);
354 /* keep compiler happy */
360 java_objectheader *exceptions_new_linkageerror(const char *message,
363 fprintf(stderr, "java.lang.LinkageError: %s",message);
365 utf_fprint_printable_ascii_classname(stderr, c->name);
371 /* keep compiler happy */
376 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
377 utf *name, utf *desc)
379 fprintf(stderr, "java.lang.NoSuchMethodError: ");
380 utf_fprint_printable_ascii(stderr, c->name);
381 fprintf(stderr, ".");
382 utf_fprint_printable_ascii(stderr, name);
383 utf_fprint_printable_ascii(stderr, desc);
388 /* keep compiler happy */
394 void exceptions_throw_nosuchmethoderror(classinfo *c, utf *name, utf *desc)
396 (void) exceptions_new_nosuchmethoderror(c, name, desc);
400 void exceptions_throw_internalerror(const char *message, ...)
404 fprintf(stderr, "%s: ", string_java_lang_InternalError);
406 va_start(ap, message);
407 vfprintf(stderr, message, ap);
414 void exceptions_throw_outofmemoryerror(void)
416 fprintf(stderr, "%s", string_java_lang_OutOfMemoryError);
421 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
425 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
427 utf_display_printable_ascii(c->name);
428 fprintf(stderr, ": ");
430 va_start(ap, message);
431 vfprintf(stderr, message, ap);
436 /* keep compiler happy */
442 void exceptions_throw_virtualmachineerror(void)
444 fprintf(stderr, "%s", string_java_lang_VirtualMachineError);
449 void exceptions_throw_arrayindexoutofboundsexception(void)
451 fprintf(stderr, "%s", string_java_lang_ArrayIndexOutOfBoundsException);
456 java_objectheader *exceptions_new_arraystoreexception(void)
458 fprintf(stderr, "%s", string_java_lang_ArrayStoreException);
461 /* keep compiler happy */
467 void exceptions_throw_arraystoreexception(void)
469 (void) exceptions_new_arraystoreexception();
473 java_objectheader *exceptions_throw_illegalmonitorstateexception(void)
475 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
478 /* keep compiler happy */
484 java_objectheader *new_negativearraysizeexception(void)
486 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
489 /* keep compiler happy */
495 void exceptions_throw_negativearraysizeexception(void)
497 (void) new_negativearraysizeexception();
501 java_objectheader *exceptions_new_nullpointerexception(void)
503 fprintf(stderr, "%s", string_java_lang_NullPointerException);
506 /* keep compiler happy */
512 void exceptions_throw_nullpointerexception(void)
514 (void) exceptions_new_nullpointerexception();
518 void classnotfoundexception_to_noclassdeffounderror(void)
522 /* machine dependent stuff ****************************************************/
524 #if defined(ENABLE_THREADS)
525 critical_section_node_t asm_criticalsections;
526 void thread_restartcriticalsection(ucontext_t *uc) {}
529 void md_param_alloc(methoddesc *md) {}
532 #if defined(ENABLE_INTRP)
533 void print_dynamic_super_statistics(void) {}
537 /************************ global variables **********************/
539 #define ACC_NATIVELY_OVERLOADED 0x10000000
541 chain *ident_chain; /* chain with method and field names in current class */
543 static u4 outputsize;
544 static bool dopadding;
547 static void printIDpart(int c)
549 if ((c >= 'a' && c <= 'z') ||
550 (c >= 'A' && c <= 'Z') ||
551 (c >= '0' && c <= '9') ||
561 char *utf_ptr = u->text;
564 for (i = 0; i < utf_get_number_of_u2s(u); i++)
565 printIDpart(utf_nextu2(&utf_ptr));
569 static void addoutputsize (int len)
572 if (!dopadding) return;
574 newsize = MEMORY_ALIGN(outputsize, len);
576 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
577 outputsize = newsize;
581 void printOverloadPart(utf *desc)
583 char *utf_ptr=desc->text;
588 while ((c = utf_nextu2(&utf_ptr)) != ')') {
598 fprintf(file, "%c", (char) c);
605 while ((c = utf_nextu2(&utf_ptr)) != ';')
612 log_text("invalid method descriptor");
618 static char *printtype(char *utf_ptr)
622 switch (utf_nextu2(&utf_ptr)) {
623 case 'V': fprintf (file, "void");
629 case 'Z': addoutputsize (4);
630 fprintf (file, "s4");
632 case 'J': addoutputsize (8);
633 fprintf (file, "s8");
635 case 'F': addoutputsize (4);
636 fprintf (file, "float");
638 case 'D': addoutputsize (8);
639 fprintf (file, "double");
642 addoutputsize ( sizeof(java_arrayheader*) );
643 switch (utf_nextu2(&utf_ptr)) {
644 case 'I': fprintf (file, "java_intarray*"); break;
645 case 'J': fprintf (file, "java_longarray*"); break;
646 case 'Z': fprintf (file, "java_booleanarray*"); break;
647 case 'B': fprintf (file, "java_bytearray*"); break;
648 case 'S': fprintf (file, "java_shortarray*"); break;
649 case 'C': fprintf (file, "java_chararray*"); break;
650 case 'F': fprintf (file, "java_floatarray*"); break;
651 case 'D': fprintf (file, "java_doublearray*"); break;
653 case '[': fprintf(file, "java_objectarray*");
654 while ((c = utf_nextu2(&utf_ptr)) == '[');
656 while (utf_nextu2(&utf_ptr) != ';');
659 case 'L': fprintf(file, "java_objectarray*");
660 while (utf_nextu2(&utf_ptr) != ';');
663 log_text("invalid type descriptor");
669 addoutputsize ( sizeof(java_objectheader*));
670 fprintf (file, "struct ");
671 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
676 log_text("Unknown type in field descriptor");
684 /***** determine the number of entries of a utf string in the ident chain *****/
686 static int searchidentchain_utf(utf *ident)
688 utf *u = chain_first(ident_chain); /* first element of list */
692 if (u==ident) count++; /* string found */
693 u = chain_next(ident_chain); /* next element in list */
700 /************** print structure for direct access to objects ******************/
702 static void printfields(classinfo *c)
709 addoutputsize(sizeof(java_objectheader));
710 fprintf(file, " java_objectheader header;\n");
714 printfields(c->super.cls);
716 for (i = 0; i < c->fieldscount; i++) {
719 if (!(f->flags & ACC_STATIC)) {
721 printtype(f->descriptor->text);
723 utf_fprint_printable_ascii(file, f->name);
725 /* rename multiple fieldnames */
726 if ((ident_count = searchidentchain_utf(f->name)))
727 fprintf(file, "%d", ident_count - 1);
728 chain_addlast(ident_chain, f->name);
730 fprintf(file, ";\n");
736 /***************** store prototype for native method in file ******************/
738 void printmethod(methodinfo *m)
743 /* search for return-type in descriptor */
744 utf_ptr = m->descriptor->text;
745 while (utf_nextu2(&utf_ptr) != ')');
748 fprintf(file, "\n/*\n * Class: ");
749 utf_fprint_printable_ascii(file, m->class->name);
750 fprintf(file, "\n * Method: ");
751 utf_fprint_printable_ascii(file, m->name);
752 fprintf(file, "\n * Signature: ");
753 utf_fprint_printable_ascii(file, m->descriptor);
754 fprintf(file, "\n */\n");
756 /* create prototype */
757 fprintf(file, "JNIEXPORT ");
759 fprintf(file, " JNICALL Java_");
760 printID(m->class->name);
762 chain_addlast(ident_chain, m->name);
767 /* ATTENTION: We use a dummy flag here. */
769 if (m->flags & ACC_NATIVELY_OVERLOADED)
770 printOverloadPart(m->descriptor);
772 fprintf(file, "(JNIEnv *env");
774 utf_ptr = m->descriptor->text + 1;
776 if (!(m->flags & ACC_STATIC)) {
777 fprintf(file, ", struct ");
778 printID(m->class->name);
779 fprintf(file, "* this");
782 fprintf(file, ", jclass clazz");
785 if ((*utf_ptr) != ')') fprintf(file, ", ");
787 while ((*utf_ptr) != ')') {
788 utf_ptr = printtype(utf_ptr);
789 fprintf(file, " par%d", paramnum++);
790 if ((*utf_ptr)!=')') fprintf(file, ", ");
793 fprintf(file, ");\n\n");
797 /******* remove package-name in fully-qualified classname *********************/
799 void gen_header_filename(char *buffer, utf *u)
803 for (i = 0; i < utf_get_number_of_u2s(u); i++) {
804 if ((u->text[i] == '/') || (u->text[i] == '$')) {
805 buffer[i] = '_'; /* convert '$' and '/' to '_' */
808 buffer[i] = u->text[i];
811 buffer[utf_get_number_of_u2s(u)] = '\0';
815 /* create headerfile for classes and store native methods in chain ************/
817 void headerfile_generate(classinfo *c, char *opt_directory)
819 char header_filename[1024] = "";
820 char classname[1024];
821 char uclassname[1024];
826 bool nativelyoverloaded;
828 /* prevent compiler warnings */
830 nativelyoverloaded = false;
832 /* open headerfile for class */
833 gen_header_filename(classname, c->name);
835 /* create chain for renaming fields */
836 ident_chain = chain_new();
839 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
842 sprintf(header_filename, "%s.h", classname);
845 file = fopen(header_filename, "w");
847 log_text("Can not open file to store header information");
851 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
853 /* convert to uppercase */
854 for (i = 0; classname[i]; i++) {
855 uclassname[i] = toupper(classname[i]);
857 uclassname[i] = '\0';
859 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
861 /* create structure for direct access to objects */
862 fprintf(file, "/* Structure information for class: ");
863 utf_fprint_printable_ascii(file, c->name);
864 fprintf(file, " */\n\n");
865 fprintf(file, "typedef struct ");
867 fprintf(file, " {\n");
875 fprintf(file, ";\n\n");
877 /* create chain for renaming overloaded methods */
878 chain_free(ident_chain);
879 ident_chain = chain_new();
881 /* create method-prototypes */
883 /* find overloaded methods */
885 for (i = 0; i < c->methodscount; i++) {
886 m = &(c->methods[i]);
888 if (!(m->flags & ACC_NATIVE))
891 /* We use a dummy flag here. */
893 if (!(m->flags & ACC_NATIVELY_OVERLOADED)) {
894 nativelyoverloaded = false;
896 for (j = i + 1; j < c->methodscount; j++) {
897 m2 = &(c->methods[j]);
899 if (!(m2->flags & ACC_NATIVE))
902 if (m->name == m2->name) {
903 m2->flags |= ACC_NATIVELY_OVERLOADED;
904 nativelyoverloaded = true;
909 if (nativelyoverloaded == true)
910 m->flags |= ACC_NATIVELY_OVERLOADED;
913 for (i = 0; i < c->methodscount; i++) {
914 m = &(c->methods[i]);
916 if (m->flags & ACC_NATIVE)
920 chain_free(ident_chain);
922 fprintf(file, "#endif\n\n");
928 /******** print classname, '$' used to seperate inner-class name ***********/
930 void print_classname(classinfo *clazz)
932 utf *u = clazz->name;
933 char *endpos = u->text + u->blength;
934 char *utf_ptr = u->text;
937 while (utf_ptr < endpos) {
938 if ((c = utf_nextu2(&utf_ptr)) == '_')
945 /* jvmti releated functions ************************************************/
947 #if defined(ENABLE_JVMTI)
948 void jvmti_ThreadStartEnd(int ev) {;}
949 void jvmti_ClassLoadPrepare(bool prepared, classinfo *c) {;}
950 void jvmti_MonitorContendedEntering(bool entered, jobject obj) {;}
956 * These are local overrides for various environment variables in Emacs.
957 * Please do not remove this and leave it at the end of the file, where
958 * Emacs will automagically detect them.
959 * ---------------------------------------------------------------------
962 * indent-tabs-mode: t
966 * vim:noexpandtab:sw=4:ts=4: