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 4598 2006-03-14 22:16:47Z 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) {}
147 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
152 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
154 return typecheck_TRUE;
157 void typeinfo_init_classinfo(typeinfo *info,classinfo *c)
161 bool typeinfo_init_class(typeinfo *info,classref_or_classinfo c)
166 void typeinfo_print(FILE *file,typeinfo *info,int indent) {}
168 void exceptions_print_exception(java_objectheader *xptr) {}
169 void stacktrace_print_trace(java_objectheader *xptr) {}
171 void code_free_code_of_method(methodinfo *m) {}
174 /* exception functions ********************************************************/
176 /* these should not be called */
178 void throw_main_exception_exit(void) { assert(0); }
179 void throw_exception(void) { assert(0); }
180 void throw_exception_exit(void) { assert(0); }
182 java_objectheader *new_verifyerror(methodinfo *m, const char *message)
186 /* keep compiler happy */
191 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
195 /* keep compiler happy */
201 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
205 fprintf(stderr, "%s: ", exception);
207 va_start(ap, message);
208 vfprintf(stderr, message, ap);
217 void exceptions_throw_outofmemory_exit(void)
219 fprintf(stderr, "java.lang.InternalError: Out of memory\n");
224 java_objectheader *new_exception(const char *classname)
226 fprintf(stderr, "%s\n", classname);
229 /* keep compiler happy */
235 java_objectheader *new_exception_message(const char *classname, const char *message)
237 fprintf(stderr, "%s: %s\n", classname, message);
240 /* keep compiler happy */
246 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
248 fprintf(stderr, "%s: ", classname);
249 utf_display(message);
254 /* keep compiler happy */
260 java_objectheader *new_exception_javastring(const char *classname,
261 java_lang_String *message)
263 fprintf(stderr, "%s: ", classname);
264 /* TODO print message */
269 /* keep compiler happy */
275 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
279 utf_display(c->name);
280 fprintf(stderr, ": ");
282 va_start(ap, message);
283 vfprintf(stderr, message, ap);
290 /* keep compiler happy */
296 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
300 va_start(ap, message);
301 (void) new_classformaterror(c, message, ap);
306 java_objectheader *new_classnotfoundexception(utf *name)
308 fprintf(stderr, "java.lang.ClassNotFoundException: ");
309 utf_fprint(stderr, name);
314 /* keep compiler happy */
320 java_objectheader *new_noclassdeffounderror(utf *name)
322 fprintf(stderr, "java.lang.NoClassDefFoundError: ");
323 utf_fprint(stderr, name);
328 /* keep compiler happy */
334 java_objectheader *exceptions_new_linkageerror(const char *message,
337 fprintf(stderr, "java.lang.LinkageError: %s",message);
339 utf_fprint_classname(stderr, c->name);
345 /* keep compiler happy */
350 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
351 utf *name, utf *desc)
353 fprintf(stderr, "java.lang.NoSuchMethodError: ");
354 utf_fprint(stderr, c->name);
355 fprintf(stderr, ".");
356 utf_fprint(stderr, name);
357 utf_fprint(stderr, desc);
362 /* keep compiler happy */
368 java_objectheader *new_internalerror(const char *message, ...)
372 fprintf(stderr, "%s: ", string_java_lang_InternalError);
374 va_start(ap, message);
375 vfprintf(stderr, message, ap);
380 /* keep compiler happy */
386 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
390 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
392 utf_display(c->name);
393 fprintf(stderr, ": ");
395 va_start(ap, message);
396 vfprintf(stderr, message, ap);
401 /* keep compiler happy */
407 java_objectheader *new_illegalmonitorstateexception(void)
409 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
412 /* keep compiler happy */
418 java_objectheader *new_negativearraysizeexception(void)
420 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
423 /* keep compiler happy */
429 void exceptions_throw_negativearraysizeexception(void)
431 (void) new_negativearraysizeexception();
435 java_objectheader *new_nullpointerexception(void)
437 fprintf(stderr, "%s", string_java_lang_NullPointerException);
440 /* keep compiler happy */
446 void exceptions_throw_nullpointerexception(void)
448 (void) new_nullpointerexception();
452 void classnotfoundexception_to_noclassdeffounderror(void)
456 /* machine dependent stuff ****************************************************/
458 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
459 threadcritnode asm_criticalsections;
460 void thread_restartcriticalsection(ucontext_t *uc) {}
463 void md_param_alloc(methoddesc *md) {}
466 #if defined(ENABLE_INTRP)
467 void print_dynamic_super_statistics(void) {}
471 /************************ global variables **********************/
473 chain *ident_chain; /* chain with method and field names in current class */
475 static u4 outputsize;
476 static bool dopadding;
479 static void printIDpart(int c)
481 if ((c >= 'a' && c <= 'z') ||
482 (c >= 'A' && c <= 'Z') ||
483 (c >= '0' && c <= '9') ||
493 char *utf_ptr = u->text;
496 for (i = 0; i < utf_strlen(u); i++)
497 printIDpart(utf_nextu2(&utf_ptr));
501 static void addoutputsize (int len)
504 if (!dopadding) return;
506 newsize = ALIGN(outputsize, len);
508 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
509 outputsize = newsize;
513 void printOverloadPart(utf *desc)
515 char *utf_ptr=desc->text;
520 while ((c = utf_nextu2(&utf_ptr)) != ')') {
530 fprintf(file, "%c", (char) c);
537 while ((c = utf_nextu2(&utf_ptr)) != ';')
544 log_text("invalid method descriptor");
550 static char *printtype(char *utf_ptr)
554 switch (utf_nextu2(&utf_ptr)) {
555 case 'V': fprintf (file, "void");
561 case 'Z': addoutputsize (4);
562 fprintf (file, "s4");
564 case 'J': addoutputsize (8);
565 fprintf (file, "s8");
567 case 'F': addoutputsize (4);
568 fprintf (file, "float");
570 case 'D': addoutputsize (8);
571 fprintf (file, "double");
574 addoutputsize ( sizeof(java_arrayheader*) );
575 switch (utf_nextu2(&utf_ptr)) {
576 case 'I': fprintf (file, "java_intarray*"); break;
577 case 'J': fprintf (file, "java_longarray*"); break;
578 case 'Z': fprintf (file, "java_booleanarray*"); break;
579 case 'B': fprintf (file, "java_bytearray*"); break;
580 case 'S': fprintf (file, "java_shortarray*"); break;
581 case 'C': fprintf (file, "java_chararray*"); break;
582 case 'F': fprintf (file, "java_floatarray*"); break;
583 case 'D': fprintf (file, "java_doublearray*"); break;
585 case '[': fprintf(file, "java_objectarray*");
586 while ((c = utf_nextu2(&utf_ptr)) == '[');
588 while (utf_nextu2(&utf_ptr) != ';');
591 case 'L': fprintf(file, "java_objectarray*");
592 while (utf_nextu2(&utf_ptr) != ';');
595 log_text("invalid type descriptor");
601 addoutputsize ( sizeof(java_objectheader*));
602 fprintf (file, "struct ");
603 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
608 log_text("Unknown type in field descriptor");
616 /***** determine the number of entries of a utf string in the ident chain *****/
618 static int searchidentchain_utf(utf *ident)
620 utf *u = chain_first(ident_chain); /* first element of list */
624 if (u==ident) count++; /* string found */
625 u = chain_next(ident_chain); /* next element in list */
632 /************** print structure for direct access to objects ******************/
634 static void printfields(classinfo *c)
641 addoutputsize(sizeof(java_objectheader));
642 fprintf(file, " java_objectheader header;\n");
646 printfields(c->super.cls);
648 for (i = 0; i < c->fieldscount; i++) {
651 if (!(f->flags & ACC_STATIC)) {
653 printtype(f->descriptor->text);
655 utf_fprint(file, f->name);
657 /* rename multiple fieldnames */
658 if ((ident_count = searchidentchain_utf(f->name)))
659 fprintf(file, "%d", ident_count - 1);
660 chain_addlast(ident_chain, f->name);
662 fprintf(file, ";\n");
668 /***************** store prototype for native method in file ******************/
670 void printmethod(methodinfo *m)
675 /* search for return-type in descriptor */
676 utf_ptr = m->descriptor->text;
677 while (utf_nextu2(&utf_ptr) != ')');
680 fprintf(file, "\n/*\n * Class: ");
681 utf_fprint(file, m->class->name);
682 fprintf(file, "\n * Method: ");
683 utf_fprint(file, m->name);
684 fprintf(file, "\n * Signature: ");
685 utf_fprint(file, m->descriptor);
686 fprintf(file, "\n */\n");
688 /* create prototype */
689 fprintf(file, "JNIEXPORT ");
691 fprintf(file, " JNICALL Java_");
692 printID(m->class->name);
694 chain_addlast(ident_chain, m->name);
699 /* ATTENTION: We use the methodinfo's isleafmethod variable as
700 nativelyoverloaded, so we can save some space during
704 printOverloadPart(m->descriptor);
706 fprintf(file, "(JNIEnv *env");
708 utf_ptr = m->descriptor->text + 1;
710 if (!(m->flags & ACC_STATIC)) {
711 fprintf(file, ", struct ");
712 printID(m->class->name);
713 fprintf(file, "* this");
716 fprintf(file, ", jclass clazz");
719 if ((*utf_ptr) != ')') fprintf(file, ", ");
721 while ((*utf_ptr) != ')') {
722 utf_ptr = printtype(utf_ptr);
723 fprintf(file, " par%d", paramnum++);
724 if ((*utf_ptr)!=')') fprintf(file, ", ");
727 fprintf(file, ");\n\n");
731 /******* remove package-name in fully-qualified classname *********************/
733 void gen_header_filename(char *buffer, utf *u)
737 for (i = 0; i < utf_strlen(u); i++) {
738 if ((u->text[i] == '/') || (u->text[i] == '$')) {
739 buffer[i] = '_'; /* convert '$' and '/' to '_' */
742 buffer[i] = u->text[i];
745 buffer[utf_strlen(u)] = '\0';
749 /* create headerfile for classes and store native methods in chain ************/
751 void headerfile_generate(classinfo *c, char *opt_directory)
753 char header_filename[1024] = "";
754 char classname[1024];
755 char uclassname[1024];
760 bool nativelyoverloaded;
762 /* prevent compiler warnings */
764 nativelyoverloaded = false;
766 /* open headerfile for class */
767 gen_header_filename(classname, c->name);
769 /* create chain for renaming fields */
770 ident_chain = chain_new();
773 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
776 sprintf(header_filename, "%s.h", classname);
779 file = fopen(header_filename, "w");
781 log_text("Can not open file to store header information");
785 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
787 /* convert to uppercase */
788 for (i = 0; classname[i]; i++) {
789 uclassname[i] = toupper(classname[i]);
791 uclassname[i] = '\0';
793 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
795 /* create structure for direct access to objects */
796 fprintf(file, "/* Structure information for class: ");
797 utf_fprint(file, c->name);
798 fprintf(file, " */\n\n");
799 fprintf(file, "typedef struct ");
801 fprintf(file, " {\n");
809 fprintf(file, ";\n\n");
811 /* create chain for renaming overloaded methods */
812 chain_free(ident_chain);
813 ident_chain = chain_new();
815 /* create method-prototypes */
817 /* find overloaded methods */
819 for (i = 0; i < c->methodscount; i++) {
820 m = &(c->methods[i]);
822 if (!(m->flags & ACC_NATIVE))
825 /* We use the methodinfo's isleafmethod variable as
826 nativelyoverloaded, so we can save some space during
829 if (!m->isleafmethod) {
830 nativelyoverloaded = false;
832 for (j = i + 1; j < c->methodscount; j++) {
833 m2 = &(c->methods[j]);
835 if (!(m2->flags & ACC_NATIVE))
838 if (m->name == m2->name) {
839 m2->isleafmethod = true;
840 nativelyoverloaded = true;
845 m->isleafmethod = nativelyoverloaded;
848 for (i = 0; i < c->methodscount; i++) {
849 m = &(c->methods[i]);
851 if (m->flags & ACC_NATIVE)
855 chain_free(ident_chain);
857 fprintf(file, "#endif\n\n");
863 /******** print classname, '$' used to seperate inner-class name ***********/
865 void print_classname(classinfo *clazz)
867 utf *u = clazz->name;
868 char *endpos = u->text + u->blength;
869 char *utf_ptr = u->text;
872 while (utf_ptr < endpos) {
873 if ((c = utf_nextu2(&utf_ptr)) == '_')
882 * These are local overrides for various environment variables in Emacs.
883 * Please do not remove this and leave it at the end of the file, where
884 * Emacs will automagically detect them.
885 * ---------------------------------------------------------------------
888 * indent-tabs-mode: t
892 * vim:noexpandtab:sw=4:ts=4: