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 5053 2006-06-28 19:11:20Z 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 #if defined(ENABLE_INTRP)
73 /* dummy interpreter stack to keep the compiler happy */
79 /* for raising exceptions from native methods *********************************/
81 #if !defined(ENABLE_THREADS)
82 java_objectheader *_no_threads_exceptionptr = NULL;
86 /* replace some non-vmcore functions ******************************************/
88 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
91 /* return something different than NULL, otherwise we get an exception */
93 return (functionptr) 1;
96 java_objectheader *native_new_and_init(classinfo *c) { return NULL; }
97 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s) { return NULL; }
98 java_objectheader *native_new_and_init_int(classinfo *c, s4 i) { return NULL; }
99 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t) { return NULL; }
102 java_objectheader *vm_call_method(methodinfo *m, java_objectheader *o, ...)
105 void vm_abort(const char *text, ...)
110 /* code patching functions */
111 void patcher_builtin_arraycheckcast(u1 *sp) {}
113 #if defined(__MIPS__)
114 long compare_and_swap(long *p, long oldval, long newval)
125 u1 *createcompilerstub(methodinfo *m) { return NULL; }
126 #if defined(ENABLE_INTRP)
127 u1 *intrp_createcompilerstub(methodinfo *m) { return NULL; }
130 codeinfo *codegen_createnativestub(functionptr f, methodinfo *m) { return NULL; }
132 void removecompilerstub(u1 *stub) {}
133 void removenativestub(u1 *stub) {}
135 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
136 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
138 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
140 void asm_handle_builtin_exception(classinfo *c) {}
141 void asm_abstractmethoderror(void) {}
143 #if defined(ENABLE_JIT)
144 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
147 #if defined(ENABLE_INTRP)
148 void intrp_asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
151 u8 asm_get_cycle_count(void)
157 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
162 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
164 return typecheck_TRUE;
167 void typeinfo_init_classinfo(typeinfo *info,classinfo *c)
171 bool typeinfo_init_class(typeinfo *info,classref_or_classinfo c)
176 void typeinfo_print(FILE *file,typeinfo *info,int indent) {}
178 void exceptions_print_exception(java_objectheader *xptr) {}
179 void stacktrace_print_trace(java_objectheader *xptr) {}
181 void code_free_code_of_method(methodinfo *m) {}
184 /* exception functions ********************************************************/
186 /* these should not be called */
188 void throw_main_exception_exit(void) { assert(0); }
189 void throw_exception(void) { assert(0); }
190 void throw_exception_exit(void) { assert(0); }
192 java_objectheader *new_verifyerror(methodinfo *m, const char *message)
196 /* keep compiler happy */
201 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
205 /* keep compiler happy */
211 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
215 fprintf(stderr, "%s: ", exception);
217 va_start(ap, message);
218 vfprintf(stderr, message, ap);
227 void exceptions_throw_outofmemory_exit(void)
229 fprintf(stderr, "java.lang.InternalError: Out of memory\n");
234 java_objectheader *new_exception(const char *classname)
236 fprintf(stderr, "%s\n", classname);
239 /* keep compiler happy */
245 java_objectheader *new_exception_message(const char *classname, const char *message)
247 fprintf(stderr, "%s: %s\n", classname, message);
250 /* keep compiler happy */
256 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
258 fprintf(stderr, "%s: ", classname);
259 utf_display_printable_ascii(message);
264 /* keep compiler happy */
270 java_objectheader *new_exception_javastring(const char *classname,
271 java_lang_String *message)
273 fprintf(stderr, "%s: ", classname);
274 /* TODO print message */
279 /* keep compiler happy */
285 void exceptions_throw_abstractmethoderror(void)
287 fprintf(stderr, "java.lang.AbstractMethodError\n");
293 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
297 utf_display_printable_ascii(c->name);
298 fprintf(stderr, ": ");
300 va_start(ap, message);
301 vfprintf(stderr, message, ap);
308 /* keep compiler happy */
314 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
318 va_start(ap, message);
319 (void) new_classformaterror(c, message, ap);
324 java_objectheader *new_classnotfoundexception(utf *name)
326 fprintf(stderr, "java.lang.ClassNotFoundException: ");
327 utf_fprint_printable_ascii(stderr, name);
332 /* keep compiler happy */
338 java_objectheader *new_noclassdeffounderror(utf *name)
340 fprintf(stderr, "java.lang.NoClassDefFoundError: ");
341 utf_fprint_printable_ascii(stderr, name);
346 /* keep compiler happy */
352 java_objectheader *exceptions_new_linkageerror(const char *message,
355 fprintf(stderr, "java.lang.LinkageError: %s",message);
357 utf_fprint_printable_ascii_classname(stderr, c->name);
363 /* keep compiler happy */
368 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
369 utf *name, utf *desc)
371 fprintf(stderr, "java.lang.NoSuchMethodError: ");
372 utf_fprint_printable_ascii(stderr, c->name);
373 fprintf(stderr, ".");
374 utf_fprint_printable_ascii(stderr, name);
375 utf_fprint_printable_ascii(stderr, desc);
380 /* keep compiler happy */
386 void exceptions_throw_nosuchmethoderror(classinfo *c, utf *name, utf *desc)
388 (void) exceptions_new_nosuchmethoderror(c, name, desc);
392 java_objectheader *new_internalerror(const char *message, ...)
396 fprintf(stderr, "%s: ", string_java_lang_InternalError);
398 va_start(ap, message);
399 vfprintf(stderr, message, ap);
404 /* keep compiler happy */
410 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
414 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
416 utf_display_printable_ascii(c->name);
417 fprintf(stderr, ": ");
419 va_start(ap, message);
420 vfprintf(stderr, message, ap);
425 /* keep compiler happy */
431 java_objectheader *new_illegalmonitorstateexception(void)
433 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
436 /* keep compiler happy */
442 java_objectheader *new_negativearraysizeexception(void)
444 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
447 /* keep compiler happy */
453 void exceptions_throw_negativearraysizeexception(void)
455 (void) new_negativearraysizeexception();
459 java_objectheader *new_nullpointerexception(void)
461 fprintf(stderr, "%s", string_java_lang_NullPointerException);
464 /* keep compiler happy */
470 void exceptions_throw_nullpointerexception(void)
472 (void) new_nullpointerexception();
476 void classnotfoundexception_to_noclassdeffounderror(void)
480 /* machine dependent stuff ****************************************************/
482 #if defined(ENABLE_THREADS)
483 critical_section_node_t asm_criticalsections;
484 void thread_restartcriticalsection(ucontext_t *uc) {}
487 void md_param_alloc(methoddesc *md) {}
490 #if defined(ENABLE_INTRP)
491 void print_dynamic_super_statistics(void) {}
495 /************************ global variables **********************/
497 chain *ident_chain; /* chain with method and field names in current class */
499 static u4 outputsize;
500 static bool dopadding;
503 static void printIDpart(int c)
505 if ((c >= 'a' && c <= 'z') ||
506 (c >= 'A' && c <= 'Z') ||
507 (c >= '0' && c <= '9') ||
517 char *utf_ptr = u->text;
520 for (i = 0; i < utf_get_number_of_u2s(u); i++)
521 printIDpart(utf_nextu2(&utf_ptr));
525 static void addoutputsize (int len)
528 if (!dopadding) return;
530 newsize = ALIGN(outputsize, len);
532 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
533 outputsize = newsize;
537 void printOverloadPart(utf *desc)
539 char *utf_ptr=desc->text;
544 while ((c = utf_nextu2(&utf_ptr)) != ')') {
554 fprintf(file, "%c", (char) c);
561 while ((c = utf_nextu2(&utf_ptr)) != ';')
568 log_text("invalid method descriptor");
574 static char *printtype(char *utf_ptr)
578 switch (utf_nextu2(&utf_ptr)) {
579 case 'V': fprintf (file, "void");
585 case 'Z': addoutputsize (4);
586 fprintf (file, "s4");
588 case 'J': addoutputsize (8);
589 fprintf (file, "s8");
591 case 'F': addoutputsize (4);
592 fprintf (file, "float");
594 case 'D': addoutputsize (8);
595 fprintf (file, "double");
598 addoutputsize ( sizeof(java_arrayheader*) );
599 switch (utf_nextu2(&utf_ptr)) {
600 case 'I': fprintf (file, "java_intarray*"); break;
601 case 'J': fprintf (file, "java_longarray*"); break;
602 case 'Z': fprintf (file, "java_booleanarray*"); break;
603 case 'B': fprintf (file, "java_bytearray*"); break;
604 case 'S': fprintf (file, "java_shortarray*"); break;
605 case 'C': fprintf (file, "java_chararray*"); break;
606 case 'F': fprintf (file, "java_floatarray*"); break;
607 case 'D': fprintf (file, "java_doublearray*"); break;
609 case '[': fprintf(file, "java_objectarray*");
610 while ((c = utf_nextu2(&utf_ptr)) == '[');
612 while (utf_nextu2(&utf_ptr) != ';');
615 case 'L': fprintf(file, "java_objectarray*");
616 while (utf_nextu2(&utf_ptr) != ';');
619 log_text("invalid type descriptor");
625 addoutputsize ( sizeof(java_objectheader*));
626 fprintf (file, "struct ");
627 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
632 log_text("Unknown type in field descriptor");
640 /***** determine the number of entries of a utf string in the ident chain *****/
642 static int searchidentchain_utf(utf *ident)
644 utf *u = chain_first(ident_chain); /* first element of list */
648 if (u==ident) count++; /* string found */
649 u = chain_next(ident_chain); /* next element in list */
656 /************** print structure for direct access to objects ******************/
658 static void printfields(classinfo *c)
665 addoutputsize(sizeof(java_objectheader));
666 fprintf(file, " java_objectheader header;\n");
670 printfields(c->super.cls);
672 for (i = 0; i < c->fieldscount; i++) {
675 if (!(f->flags & ACC_STATIC)) {
677 printtype(f->descriptor->text);
679 utf_fprint_printable_ascii(file, f->name);
681 /* rename multiple fieldnames */
682 if ((ident_count = searchidentchain_utf(f->name)))
683 fprintf(file, "%d", ident_count - 1);
684 chain_addlast(ident_chain, f->name);
686 fprintf(file, ";\n");
692 /***************** store prototype for native method in file ******************/
694 void printmethod(methodinfo *m)
699 /* search for return-type in descriptor */
700 utf_ptr = m->descriptor->text;
701 while (utf_nextu2(&utf_ptr) != ')');
704 fprintf(file, "\n/*\n * Class: ");
705 utf_fprint_printable_ascii(file, m->class->name);
706 fprintf(file, "\n * Method: ");
707 utf_fprint_printable_ascii(file, m->name);
708 fprintf(file, "\n * Signature: ");
709 utf_fprint_printable_ascii(file, m->descriptor);
710 fprintf(file, "\n */\n");
712 /* create prototype */
713 fprintf(file, "JNIEXPORT ");
715 fprintf(file, " JNICALL Java_");
716 printID(m->class->name);
718 chain_addlast(ident_chain, m->name);
723 /* ATTENTION: We use the methodinfo's isleafmethod variable as
724 nativelyoverloaded, so we can save some space during
728 printOverloadPart(m->descriptor);
730 fprintf(file, "(JNIEnv *env");
732 utf_ptr = m->descriptor->text + 1;
734 if (!(m->flags & ACC_STATIC)) {
735 fprintf(file, ", struct ");
736 printID(m->class->name);
737 fprintf(file, "* this");
740 fprintf(file, ", jclass clazz");
743 if ((*utf_ptr) != ')') fprintf(file, ", ");
745 while ((*utf_ptr) != ')') {
746 utf_ptr = printtype(utf_ptr);
747 fprintf(file, " par%d", paramnum++);
748 if ((*utf_ptr)!=')') fprintf(file, ", ");
751 fprintf(file, ");\n\n");
755 /******* remove package-name in fully-qualified classname *********************/
757 void gen_header_filename(char *buffer, utf *u)
761 for (i = 0; i < utf_get_number_of_u2s(u); i++) {
762 if ((u->text[i] == '/') || (u->text[i] == '$')) {
763 buffer[i] = '_'; /* convert '$' and '/' to '_' */
766 buffer[i] = u->text[i];
769 buffer[utf_get_number_of_u2s(u)] = '\0';
773 /* create headerfile for classes and store native methods in chain ************/
775 void headerfile_generate(classinfo *c, char *opt_directory)
777 char header_filename[1024] = "";
778 char classname[1024];
779 char uclassname[1024];
784 bool nativelyoverloaded;
786 /* prevent compiler warnings */
788 nativelyoverloaded = false;
790 /* open headerfile for class */
791 gen_header_filename(classname, c->name);
793 /* create chain for renaming fields */
794 ident_chain = chain_new();
797 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
800 sprintf(header_filename, "%s.h", classname);
803 file = fopen(header_filename, "w");
805 log_text("Can not open file to store header information");
809 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
811 /* convert to uppercase */
812 for (i = 0; classname[i]; i++) {
813 uclassname[i] = toupper(classname[i]);
815 uclassname[i] = '\0';
817 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
819 /* create structure for direct access to objects */
820 fprintf(file, "/* Structure information for class: ");
821 utf_fprint_printable_ascii(file, c->name);
822 fprintf(file, " */\n\n");
823 fprintf(file, "typedef struct ");
825 fprintf(file, " {\n");
833 fprintf(file, ";\n\n");
835 /* create chain for renaming overloaded methods */
836 chain_free(ident_chain);
837 ident_chain = chain_new();
839 /* create method-prototypes */
841 /* find overloaded methods */
843 for (i = 0; i < c->methodscount; i++) {
844 m = &(c->methods[i]);
846 if (!(m->flags & ACC_NATIVE))
849 /* We use the methodinfo's isleafmethod variable as
850 nativelyoverloaded, so we can save some space during
853 if (!m->isleafmethod) {
854 nativelyoverloaded = false;
856 for (j = i + 1; j < c->methodscount; j++) {
857 m2 = &(c->methods[j]);
859 if (!(m2->flags & ACC_NATIVE))
862 if (m->name == m2->name) {
863 m2->isleafmethod = true;
864 nativelyoverloaded = true;
869 m->isleafmethod = nativelyoverloaded;
872 for (i = 0; i < c->methodscount; i++) {
873 m = &(c->methods[i]);
875 if (m->flags & ACC_NATIVE)
879 chain_free(ident_chain);
881 fprintf(file, "#endif\n\n");
887 /******** print classname, '$' used to seperate inner-class name ***********/
889 void print_classname(classinfo *clazz)
891 utf *u = clazz->name;
892 char *endpos = u->text + u->blength;
893 char *utf_ptr = u->text;
896 while (utf_ptr < endpos) {
897 if ((c = utf_nextu2(&utf_ptr)) == '_')
904 /* jvmti releated functions ************************************************/
906 #if defined(ENABLE_JVMTI)
907 void jvmti_ThreadStartEnd(int ev) {;}
908 void jvmti_ClassLoadPrepare(bool prepared, classinfo *c) {;}
909 void jvmti_MonitorContendedEntering(bool entered, jobject obj) {;}
915 * These are local overrides for various environment variables in Emacs.
916 * Please do not remove this and leave it at the end of the file, where
917 * Emacs will automagically detect them.
918 * ---------------------------------------------------------------------
921 * indent-tabs-mode: t
925 * vim:noexpandtab:sw=4:ts=4: