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
33 $Id: headers.c 4570 2006-03-07 18:18:44Z twisti $
47 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
48 # if defined(__DARWIN__)
51 # include <ucontext.h>
55 #include "mm/memory.h"
56 #include "native/include/java_lang_String.h"
57 #include "native/include/java_lang_Throwable.h"
58 #include "toolbox/chain.h"
59 #include "toolbox/logging.h"
60 #include "vm/builtin.h"
62 #include "vm/global.h"
63 #include "vm/method.h"
64 #include "vm/loader.h"
65 #include "vm/options.h"
66 #include "vm/stringlocal.h"
67 #include "vm/jit/asmpart.h"
70 #if defined(ENABLE_INTRP)
71 /* dummy interpreter stack to keep the compiler happy */
77 /* for raising exceptions from native methods *********************************/
79 #if !defined(USE_THREADS) || !defined(NATIVE_THREADS)
80 java_objectheader *_no_threads_exceptionptr = NULL;
84 /* replace some non-vmcore functions ******************************************/
86 functionptr native_findfunction(utf *cname, utf *mname, utf *desc,
89 /* return something different than NULL, otherwise we get an exception */
91 return (functionptr) 1;
94 java_objectheader *native_new_and_init(classinfo *c) { return NULL; }
95 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s) { return NULL; }
96 java_objectheader *native_new_and_init_int(classinfo *c, s4 i) { return NULL; }
97 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t) { return NULL; }
100 java_objectheader *vm_call_method(methodinfo *m, java_objectheader *o, ...)
104 /* code patching functions */
105 void patcher_builtin_arraycheckcast(u1 *sp) {}
107 #if defined(__MIPS__)
108 long compare_and_swap(long *p, long oldval, long newval)
119 u1 *createcompilerstub(methodinfo *m) { return NULL; }
120 #if defined(ENABLE_INTRP)
121 u1 *intrp_createcompilerstub(methodinfo *m) { return NULL; }
124 u1 *codegen_createnativestub(functionptr f, methodinfo *m) { return NULL; }
126 void removecompilerstub(u1 *stub) {}
127 void removenativestub(u1 *stub) {}
129 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
130 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
132 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
134 void asm_handle_builtin_exception(classinfo *c) {}
137 #if defined(ENABLE_JIT)
138 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
141 #if defined(ENABLE_INTRP)
142 void intrp_asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
146 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
151 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
153 return typecheck_TRUE;
156 void typeinfo_init_classinfo(typeinfo *info,classinfo *c)
160 bool typeinfo_init_class(typeinfo *info,classref_or_classinfo c)
165 void typeinfo_print(FILE *file,typeinfo *info,int indent) {}
167 void exceptions_print_exception(java_objectheader *xptr) {}
168 void stacktrace_print_trace(java_objectheader *xptr) {}
171 /* exception functions ********************************************************/
173 /* these should not be called */
175 void throw_main_exception_exit(void) { assert(0); }
176 void throw_exception(void) { assert(0); }
177 void throw_exception_exit(void) { assert(0); }
179 java_objectheader *new_verifyerror(methodinfo *m, const char *message)
183 /* keep compiler happy */
188 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
192 /* keep compiler happy */
198 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
202 fprintf(stderr, "%s: ", exception);
204 va_start(ap, message);
205 vfprintf(stderr, message, ap);
214 void exceptions_throw_outofmemory_exit(void)
216 fprintf(stderr, "java.lang.InternalError: Out of memory\n");
221 java_objectheader *new_exception(const char *classname)
223 fprintf(stderr, "%s\n", classname);
226 /* keep compiler happy */
232 java_objectheader *new_exception_message(const char *classname, const char *message)
234 fprintf(stderr, "%s: %s\n", classname, message);
237 /* keep compiler happy */
243 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
245 fprintf(stderr, "%s: ", classname);
246 utf_display(message);
251 /* keep compiler happy */
257 java_objectheader *new_exception_javastring(const char *classname,
258 java_lang_String *message)
260 fprintf(stderr, "%s: ", classname);
261 /* TODO print message */
266 /* keep compiler happy */
272 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
276 utf_display(c->name);
277 fprintf(stderr, ": ");
279 va_start(ap, message);
280 vfprintf(stderr, message, ap);
287 /* keep compiler happy */
293 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
297 va_start(ap, message);
298 (void) new_classformaterror(c, message, ap);
303 java_objectheader *new_classnotfoundexception(utf *name)
305 fprintf(stderr, "java.lang.ClassNotFoundException: ");
306 utf_fprint(stderr, name);
311 /* keep compiler happy */
317 java_objectheader *new_noclassdeffounderror(utf *name)
319 fprintf(stderr, "java.lang.NoClassDefFoundError: ");
320 utf_fprint(stderr, name);
325 /* keep compiler happy */
331 java_objectheader *exceptions_new_linkageerror(const char *message,
334 fprintf(stderr, "java.lang.LinkageError: %s",message);
336 utf_fprint_classname(stderr, c->name);
342 /* keep compiler happy */
347 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
348 utf *name, utf *desc)
350 fprintf(stderr, "java.lang.NoSuchMethodError: ");
351 utf_fprint(stderr, c->name);
352 fprintf(stderr, ".");
353 utf_fprint(stderr, name);
354 utf_fprint(stderr, desc);
359 /* keep compiler happy */
365 java_objectheader *new_internalerror(const char *message, ...)
369 fprintf(stderr, "%s: ", string_java_lang_InternalError);
371 va_start(ap, message);
372 vfprintf(stderr, message, ap);
377 /* keep compiler happy */
383 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
387 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
389 utf_display(c->name);
390 fprintf(stderr, ": ");
392 va_start(ap, message);
393 vfprintf(stderr, message, ap);
398 /* keep compiler happy */
404 java_objectheader *new_illegalmonitorstateexception(void)
406 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
409 /* keep compiler happy */
415 java_objectheader *new_negativearraysizeexception(void)
417 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
420 /* keep compiler happy */
426 void exceptions_throw_negativearraysizeexception(void)
428 (void) new_negativearraysizeexception();
432 java_objectheader *new_nullpointerexception(void)
434 fprintf(stderr, "%s", string_java_lang_NullPointerException);
437 /* keep compiler happy */
443 void exceptions_throw_nullpointerexception(void)
445 (void) new_nullpointerexception();
449 void classnotfoundexception_to_noclassdeffounderror(void)
453 /* machine dependent stuff ****************************************************/
455 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
456 threadcritnode asm_criticalsections;
457 void thread_restartcriticalsection(ucontext_t *uc) {}
460 void md_param_alloc(methoddesc *md) {}
463 #if defined(ENABLE_INTRP)
464 void print_dynamic_super_statistics(void) {}
468 /************************ global variables **********************/
470 chain *ident_chain; /* chain with method and field names in current class */
472 static u4 outputsize;
473 static bool dopadding;
476 static void printIDpart(int c)
478 if ((c >= 'a' && c <= 'z') ||
479 (c >= 'A' && c <= 'Z') ||
480 (c >= '0' && c <= '9') ||
490 char *utf_ptr = u->text;
493 for (i = 0; i < utf_strlen(u); i++)
494 printIDpart(utf_nextu2(&utf_ptr));
498 static void addoutputsize (int len)
501 if (!dopadding) return;
503 newsize = ALIGN(outputsize, len);
505 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
506 outputsize = newsize;
510 void printOverloadPart(utf *desc)
512 char *utf_ptr=desc->text;
517 while ((c = utf_nextu2(&utf_ptr)) != ')') {
527 fprintf(file, "%c", (char) c);
534 while ((c = utf_nextu2(&utf_ptr)) != ';')
541 log_text("invalid method descriptor");
547 static char *printtype(char *utf_ptr)
551 switch (utf_nextu2(&utf_ptr)) {
552 case 'V': fprintf (file, "void");
558 case 'Z': addoutputsize (4);
559 fprintf (file, "s4");
561 case 'J': addoutputsize (8);
562 fprintf (file, "s8");
564 case 'F': addoutputsize (4);
565 fprintf (file, "float");
567 case 'D': addoutputsize (8);
568 fprintf (file, "double");
571 addoutputsize ( sizeof(java_arrayheader*) );
572 switch (utf_nextu2(&utf_ptr)) {
573 case 'I': fprintf (file, "java_intarray*"); break;
574 case 'J': fprintf (file, "java_longarray*"); break;
575 case 'Z': fprintf (file, "java_booleanarray*"); break;
576 case 'B': fprintf (file, "java_bytearray*"); break;
577 case 'S': fprintf (file, "java_shortarray*"); break;
578 case 'C': fprintf (file, "java_chararray*"); break;
579 case 'F': fprintf (file, "java_floatarray*"); break;
580 case 'D': fprintf (file, "java_doublearray*"); break;
582 case '[': fprintf(file, "java_objectarray*");
583 while ((c = utf_nextu2(&utf_ptr)) == '[');
585 while (utf_nextu2(&utf_ptr) != ';');
588 case 'L': fprintf(file, "java_objectarray*");
589 while (utf_nextu2(&utf_ptr) != ';');
592 log_text("invalid type descriptor");
598 addoutputsize ( sizeof(java_objectheader*));
599 fprintf (file, "struct ");
600 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
605 log_text("Unknown type in field descriptor");
613 /***** determine the number of entries of a utf string in the ident chain *****/
615 static int searchidentchain_utf(utf *ident)
617 utf *u = chain_first(ident_chain); /* first element of list */
621 if (u==ident) count++; /* string found */
622 u = chain_next(ident_chain); /* next element in list */
629 /************** print structure for direct access to objects ******************/
631 static void printfields(classinfo *c)
638 addoutputsize(sizeof(java_objectheader));
639 fprintf(file, " java_objectheader header;\n");
643 printfields(c->super.cls);
645 for (i = 0; i < c->fieldscount; i++) {
648 if (!(f->flags & ACC_STATIC)) {
650 printtype(f->descriptor->text);
652 utf_fprint(file, f->name);
654 /* rename multiple fieldnames */
655 if ((ident_count = searchidentchain_utf(f->name)))
656 fprintf(file, "%d", ident_count - 1);
657 chain_addlast(ident_chain, f->name);
659 fprintf(file, ";\n");
665 /***************** store prototype for native method in file ******************/
667 void printmethod(methodinfo *m)
672 /* search for return-type in descriptor */
673 utf_ptr = m->descriptor->text;
674 while (utf_nextu2(&utf_ptr) != ')');
677 fprintf(file, "\n/*\n * Class: ");
678 utf_fprint(file, m->class->name);
679 fprintf(file, "\n * Method: ");
680 utf_fprint(file, m->name);
681 fprintf(file, "\n * Signature: ");
682 utf_fprint(file, m->descriptor);
683 fprintf(file, "\n */\n");
685 /* create prototype */
686 fprintf(file, "JNIEXPORT ");
688 fprintf(file, " JNICALL Java_");
689 printID(m->class->name);
691 chain_addlast(ident_chain, m->name);
696 /* ATTENTION: We use the methodinfo's isleafmethod variable as
697 nativelyoverloaded, so we can save some space during
701 printOverloadPart(m->descriptor);
703 fprintf(file, "(JNIEnv *env");
705 utf_ptr = m->descriptor->text + 1;
707 if (!(m->flags & ACC_STATIC)) {
708 fprintf(file, ", struct ");
709 printID(m->class->name);
710 fprintf(file, "* this");
713 fprintf(file, ", jclass clazz");
716 if ((*utf_ptr) != ')') fprintf(file, ", ");
718 while ((*utf_ptr) != ')') {
719 utf_ptr = printtype(utf_ptr);
720 fprintf(file, " par%d", paramnum++);
721 if ((*utf_ptr)!=')') fprintf(file, ", ");
724 fprintf(file, ");\n\n");
728 /******* remove package-name in fully-qualified classname *********************/
730 void gen_header_filename(char *buffer, utf *u)
734 for (i = 0; i < utf_strlen(u); i++) {
735 if ((u->text[i] == '/') || (u->text[i] == '$')) {
736 buffer[i] = '_'; /* convert '$' and '/' to '_' */
739 buffer[i] = u->text[i];
742 buffer[utf_strlen(u)] = '\0';
746 /* create headerfile for classes and store native methods in chain ************/
748 void headerfile_generate(classinfo *c, char *opt_directory)
750 char header_filename[1024] = "";
751 char classname[1024];
752 char uclassname[1024];
757 bool nativelyoverloaded;
759 /* prevent compiler warnings */
761 nativelyoverloaded = false;
763 /* open headerfile for class */
764 gen_header_filename(classname, c->name);
766 /* create chain for renaming fields */
767 ident_chain = chain_new();
770 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
773 sprintf(header_filename, "%s.h", classname);
776 file = fopen(header_filename, "w");
778 log_text("Can not open file to store header information");
782 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
784 /* convert to uppercase */
785 for (i = 0; classname[i]; i++) {
786 uclassname[i] = toupper(classname[i]);
788 uclassname[i] = '\0';
790 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
792 /* create structure for direct access to objects */
793 fprintf(file, "/* Structure information for class: ");
794 utf_fprint(file, c->name);
795 fprintf(file, " */\n\n");
796 fprintf(file, "typedef struct ");
798 fprintf(file, " {\n");
806 fprintf(file, ";\n\n");
808 /* create chain for renaming overloaded methods */
809 chain_free(ident_chain);
810 ident_chain = chain_new();
812 /* create method-prototypes */
814 /* find overloaded methods */
816 for (i = 0; i < c->methodscount; i++) {
817 m = &(c->methods[i]);
819 if (!(m->flags & ACC_NATIVE))
822 /* We use the methodinfo's isleafmethod variable as
823 nativelyoverloaded, so we can save some space during
826 if (!m->isleafmethod) {
827 nativelyoverloaded = false;
829 for (j = i + 1; j < c->methodscount; j++) {
830 m2 = &(c->methods[j]);
832 if (!(m2->flags & ACC_NATIVE))
835 if (m->name == m2->name) {
836 m2->isleafmethod = true;
837 nativelyoverloaded = true;
842 m->isleafmethod = nativelyoverloaded;
845 for (i = 0; i < c->methodscount; i++) {
846 m = &(c->methods[i]);
848 if (m->flags & ACC_NATIVE)
852 chain_free(ident_chain);
854 fprintf(file, "#endif\n\n");
860 /******** print classname, '$' used to seperate inner-class name ***********/
862 void print_classname(classinfo *clazz)
864 utf *u = clazz->name;
865 char *endpos = u->text + u->blength;
866 char *utf_ptr = u->text;
869 while (utf_ptr < endpos) {
870 if ((c = utf_nextu2(&utf_ptr)) == '_')
879 * These are local overrides for various environment variables in Emacs.
880 * Please do not remove this and leave it at the end of the file, where
881 * Emacs will automagically detect them.
882 * ---------------------------------------------------------------------
885 * indent-tabs-mode: t