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 4908 2006-05-12 16:49:50Z edwin $
48 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
49 # if defined(__DARWIN__)
52 # include <ucontext.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 #if defined(ENABLE_INTRP)
72 /* dummy interpreter stack to keep the compiler happy */
78 /* for raising exceptions from native methods *********************************/
80 #if !defined(USE_THREADS) || !defined(NATIVE_THREADS)
81 java_objectheader *_no_threads_exceptionptr = NULL;
85 /* replace some non-vmcore functions ******************************************/
87 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
90 /* return something different than NULL, otherwise we get an exception */
92 return (functionptr) 1;
95 java_objectheader *native_new_and_init(classinfo *c) { return NULL; }
96 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s) { return NULL; }
97 java_objectheader *native_new_and_init_int(classinfo *c, s4 i) { return NULL; }
98 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t) { return NULL; }
101 java_objectheader *vm_call_method(methodinfo *m, java_objectheader *o, ...)
105 /* code patching functions */
106 void patcher_builtin_arraycheckcast(u1 *sp) {}
108 #if defined(__MIPS__)
109 long compare_and_swap(long *p, long oldval, long newval)
120 u1 *createcompilerstub(methodinfo *m) { return NULL; }
121 #if defined(ENABLE_INTRP)
122 u1 *intrp_createcompilerstub(methodinfo *m) { return NULL; }
125 codeinfo *codegen_createnativestub(functionptr f, methodinfo *m) { return NULL; }
127 void removecompilerstub(u1 *stub) {}
128 void removenativestub(u1 *stub) {}
130 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
131 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
133 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
135 void asm_handle_builtin_exception(classinfo *c) {}
138 #if defined(ENABLE_JIT)
139 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
142 #if defined(ENABLE_INTRP)
143 void intrp_asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
146 u8 asm_get_cycle_count(void)
152 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
157 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
159 return typecheck_TRUE;
162 void typeinfo_init_classinfo(typeinfo *info,classinfo *c)
166 bool typeinfo_init_class(typeinfo *info,classref_or_classinfo c)
171 void typeinfo_print(FILE *file,typeinfo *info,int indent) {}
173 void exceptions_print_exception(java_objectheader *xptr) {}
174 void stacktrace_print_trace(java_objectheader *xptr) {}
176 void code_free_code_of_method(methodinfo *m) {}
179 /* exception functions ********************************************************/
181 /* these should not be called */
183 void throw_main_exception_exit(void) { assert(0); }
184 void throw_exception(void) { assert(0); }
185 void throw_exception_exit(void) { assert(0); }
187 java_objectheader *new_verifyerror(methodinfo *m, const char *message)
191 /* keep compiler happy */
196 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
200 /* keep compiler happy */
206 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
210 fprintf(stderr, "%s: ", exception);
212 va_start(ap, message);
213 vfprintf(stderr, message, ap);
222 void exceptions_throw_outofmemory_exit(void)
224 fprintf(stderr, "java.lang.InternalError: Out of memory\n");
229 java_objectheader *new_exception(const char *classname)
231 fprintf(stderr, "%s\n", classname);
234 /* keep compiler happy */
240 java_objectheader *new_exception_message(const char *classname, const char *message)
242 fprintf(stderr, "%s: %s\n", classname, message);
245 /* keep compiler happy */
251 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
253 fprintf(stderr, "%s: ", classname);
254 utf_display_printable_ascii(message);
259 /* keep compiler happy */
265 java_objectheader *new_exception_javastring(const char *classname,
266 java_lang_String *message)
268 fprintf(stderr, "%s: ", classname);
269 /* TODO print message */
274 /* keep compiler happy */
280 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
284 utf_display_printable_ascii(c->name);
285 fprintf(stderr, ": ");
287 va_start(ap, message);
288 vfprintf(stderr, message, ap);
295 /* keep compiler happy */
301 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
305 va_start(ap, message);
306 (void) new_classformaterror(c, message, ap);
311 java_objectheader *new_classnotfoundexception(utf *name)
313 fprintf(stderr, "java.lang.ClassNotFoundException: ");
314 utf_fprint_printable_ascii(stderr, name);
319 /* keep compiler happy */
325 java_objectheader *new_noclassdeffounderror(utf *name)
327 fprintf(stderr, "java.lang.NoClassDefFoundError: ");
328 utf_fprint_printable_ascii(stderr, name);
333 /* keep compiler happy */
339 java_objectheader *exceptions_new_linkageerror(const char *message,
342 fprintf(stderr, "java.lang.LinkageError: %s",message);
344 utf_fprint_printable_ascii_classname(stderr, c->name);
350 /* keep compiler happy */
355 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
356 utf *name, utf *desc)
358 fprintf(stderr, "java.lang.NoSuchMethodError: ");
359 utf_fprint_printable_ascii(stderr, c->name);
360 fprintf(stderr, ".");
361 utf_fprint_printable_ascii(stderr, name);
362 utf_fprint_printable_ascii(stderr, desc);
367 /* keep compiler happy */
373 java_objectheader *new_internalerror(const char *message, ...)
377 fprintf(stderr, "%s: ", string_java_lang_InternalError);
379 va_start(ap, message);
380 vfprintf(stderr, message, ap);
385 /* keep compiler happy */
391 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
395 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
397 utf_display_printable_ascii(c->name);
398 fprintf(stderr, ": ");
400 va_start(ap, message);
401 vfprintf(stderr, message, ap);
406 /* keep compiler happy */
412 java_objectheader *new_illegalmonitorstateexception(void)
414 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
417 /* keep compiler happy */
423 java_objectheader *new_negativearraysizeexception(void)
425 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
428 /* keep compiler happy */
434 void exceptions_throw_negativearraysizeexception(void)
436 (void) new_negativearraysizeexception();
440 java_objectheader *new_nullpointerexception(void)
442 fprintf(stderr, "%s", string_java_lang_NullPointerException);
445 /* keep compiler happy */
451 void exceptions_throw_nullpointerexception(void)
453 (void) new_nullpointerexception();
457 void classnotfoundexception_to_noclassdeffounderror(void)
461 /* machine dependent stuff ****************************************************/
463 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
464 critical_section_node_t asm_criticalsections;
465 void thread_restartcriticalsection(ucontext_t *uc) {}
468 void md_param_alloc(methoddesc *md) {}
471 #if defined(ENABLE_INTRP)
472 void print_dynamic_super_statistics(void) {}
476 /************************ global variables **********************/
478 chain *ident_chain; /* chain with method and field names in current class */
480 static u4 outputsize;
481 static bool dopadding;
484 static void printIDpart(int c)
486 if ((c >= 'a' && c <= 'z') ||
487 (c >= 'A' && c <= 'Z') ||
488 (c >= '0' && c <= '9') ||
498 char *utf_ptr = u->text;
501 for (i = 0; i < utf_get_number_of_u2s(u); i++)
502 printIDpart(utf_nextu2(&utf_ptr));
506 static void addoutputsize (int len)
509 if (!dopadding) return;
511 newsize = ALIGN(outputsize, len);
513 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
514 outputsize = newsize;
518 void printOverloadPart(utf *desc)
520 char *utf_ptr=desc->text;
525 while ((c = utf_nextu2(&utf_ptr)) != ')') {
535 fprintf(file, "%c", (char) c);
542 while ((c = utf_nextu2(&utf_ptr)) != ';')
549 log_text("invalid method descriptor");
555 static char *printtype(char *utf_ptr)
559 switch (utf_nextu2(&utf_ptr)) {
560 case 'V': fprintf (file, "void");
566 case 'Z': addoutputsize (4);
567 fprintf (file, "s4");
569 case 'J': addoutputsize (8);
570 fprintf (file, "s8");
572 case 'F': addoutputsize (4);
573 fprintf (file, "float");
575 case 'D': addoutputsize (8);
576 fprintf (file, "double");
579 addoutputsize ( sizeof(java_arrayheader*) );
580 switch (utf_nextu2(&utf_ptr)) {
581 case 'I': fprintf (file, "java_intarray*"); break;
582 case 'J': fprintf (file, "java_longarray*"); break;
583 case 'Z': fprintf (file, "java_booleanarray*"); break;
584 case 'B': fprintf (file, "java_bytearray*"); break;
585 case 'S': fprintf (file, "java_shortarray*"); break;
586 case 'C': fprintf (file, "java_chararray*"); break;
587 case 'F': fprintf (file, "java_floatarray*"); break;
588 case 'D': fprintf (file, "java_doublearray*"); break;
590 case '[': fprintf(file, "java_objectarray*");
591 while ((c = utf_nextu2(&utf_ptr)) == '[');
593 while (utf_nextu2(&utf_ptr) != ';');
596 case 'L': fprintf(file, "java_objectarray*");
597 while (utf_nextu2(&utf_ptr) != ';');
600 log_text("invalid type descriptor");
606 addoutputsize ( sizeof(java_objectheader*));
607 fprintf (file, "struct ");
608 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
613 log_text("Unknown type in field descriptor");
621 /***** determine the number of entries of a utf string in the ident chain *****/
623 static int searchidentchain_utf(utf *ident)
625 utf *u = chain_first(ident_chain); /* first element of list */
629 if (u==ident) count++; /* string found */
630 u = chain_next(ident_chain); /* next element in list */
637 /************** print structure for direct access to objects ******************/
639 static void printfields(classinfo *c)
646 addoutputsize(sizeof(java_objectheader));
647 fprintf(file, " java_objectheader header;\n");
651 printfields(c->super.cls);
653 for (i = 0; i < c->fieldscount; i++) {
656 if (!(f->flags & ACC_STATIC)) {
658 printtype(f->descriptor->text);
660 utf_fprint_printable_ascii(file, f->name);
662 /* rename multiple fieldnames */
663 if ((ident_count = searchidentchain_utf(f->name)))
664 fprintf(file, "%d", ident_count - 1);
665 chain_addlast(ident_chain, f->name);
667 fprintf(file, ";\n");
673 /***************** store prototype for native method in file ******************/
675 void printmethod(methodinfo *m)
680 /* search for return-type in descriptor */
681 utf_ptr = m->descriptor->text;
682 while (utf_nextu2(&utf_ptr) != ')');
685 fprintf(file, "\n/*\n * Class: ");
686 utf_fprint_printable_ascii(file, m->class->name);
687 fprintf(file, "\n * Method: ");
688 utf_fprint_printable_ascii(file, m->name);
689 fprintf(file, "\n * Signature: ");
690 utf_fprint_printable_ascii(file, m->descriptor);
691 fprintf(file, "\n */\n");
693 /* create prototype */
694 fprintf(file, "JNIEXPORT ");
696 fprintf(file, " JNICALL Java_");
697 printID(m->class->name);
699 chain_addlast(ident_chain, m->name);
704 /* ATTENTION: We use the methodinfo's isleafmethod variable as
705 nativelyoverloaded, so we can save some space during
709 printOverloadPart(m->descriptor);
711 fprintf(file, "(JNIEnv *env");
713 utf_ptr = m->descriptor->text + 1;
715 if (!(m->flags & ACC_STATIC)) {
716 fprintf(file, ", struct ");
717 printID(m->class->name);
718 fprintf(file, "* this");
721 fprintf(file, ", jclass clazz");
724 if ((*utf_ptr) != ')') fprintf(file, ", ");
726 while ((*utf_ptr) != ')') {
727 utf_ptr = printtype(utf_ptr);
728 fprintf(file, " par%d", paramnum++);
729 if ((*utf_ptr)!=')') fprintf(file, ", ");
732 fprintf(file, ");\n\n");
736 /******* remove package-name in fully-qualified classname *********************/
738 void gen_header_filename(char *buffer, utf *u)
742 for (i = 0; i < utf_get_number_of_u2s(u); i++) {
743 if ((u->text[i] == '/') || (u->text[i] == '$')) {
744 buffer[i] = '_'; /* convert '$' and '/' to '_' */
747 buffer[i] = u->text[i];
750 buffer[utf_get_number_of_u2s(u)] = '\0';
754 /* create headerfile for classes and store native methods in chain ************/
756 void headerfile_generate(classinfo *c, char *opt_directory)
758 char header_filename[1024] = "";
759 char classname[1024];
760 char uclassname[1024];
765 bool nativelyoverloaded;
767 /* prevent compiler warnings */
769 nativelyoverloaded = false;
771 /* open headerfile for class */
772 gen_header_filename(classname, c->name);
774 /* create chain for renaming fields */
775 ident_chain = chain_new();
778 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
781 sprintf(header_filename, "%s.h", classname);
784 file = fopen(header_filename, "w");
786 log_text("Can not open file to store header information");
790 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
792 /* convert to uppercase */
793 for (i = 0; classname[i]; i++) {
794 uclassname[i] = toupper(classname[i]);
796 uclassname[i] = '\0';
798 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
800 /* create structure for direct access to objects */
801 fprintf(file, "/* Structure information for class: ");
802 utf_fprint_printable_ascii(file, c->name);
803 fprintf(file, " */\n\n");
804 fprintf(file, "typedef struct ");
806 fprintf(file, " {\n");
814 fprintf(file, ";\n\n");
816 /* create chain for renaming overloaded methods */
817 chain_free(ident_chain);
818 ident_chain = chain_new();
820 /* create method-prototypes */
822 /* find overloaded methods */
824 for (i = 0; i < c->methodscount; i++) {
825 m = &(c->methods[i]);
827 if (!(m->flags & ACC_NATIVE))
830 /* We use the methodinfo's isleafmethod variable as
831 nativelyoverloaded, so we can save some space during
834 if (!m->isleafmethod) {
835 nativelyoverloaded = false;
837 for (j = i + 1; j < c->methodscount; j++) {
838 m2 = &(c->methods[j]);
840 if (!(m2->flags & ACC_NATIVE))
843 if (m->name == m2->name) {
844 m2->isleafmethod = true;
845 nativelyoverloaded = true;
850 m->isleafmethod = nativelyoverloaded;
853 for (i = 0; i < c->methodscount; i++) {
854 m = &(c->methods[i]);
856 if (m->flags & ACC_NATIVE)
860 chain_free(ident_chain);
862 fprintf(file, "#endif\n\n");
868 /******** print classname, '$' used to seperate inner-class name ***********/
870 void print_classname(classinfo *clazz)
872 utf *u = clazz->name;
873 char *endpos = u->text + u->blength;
874 char *utf_ptr = u->text;
877 while (utf_ptr < endpos) {
878 if ((c = utf_nextu2(&utf_ptr)) == '_')
887 * These are local overrides for various environment variables in Emacs.
888 * Please do not remove this and leave it at the end of the file, where
889 * Emacs will automagically detect them.
890 * ---------------------------------------------------------------------
893 * indent-tabs-mode: t
897 * vim:noexpandtab:sw=4:ts=4: