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 4938 2006-05-22 09:06:44Z 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) {}
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 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
289 utf_display_printable_ascii(c->name);
290 fprintf(stderr, ": ");
292 va_start(ap, message);
293 vfprintf(stderr, message, ap);
300 /* keep compiler happy */
306 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
310 va_start(ap, message);
311 (void) new_classformaterror(c, message, ap);
316 java_objectheader *new_classnotfoundexception(utf *name)
318 fprintf(stderr, "java.lang.ClassNotFoundException: ");
319 utf_fprint_printable_ascii(stderr, name);
324 /* keep compiler happy */
330 java_objectheader *new_noclassdeffounderror(utf *name)
332 fprintf(stderr, "java.lang.NoClassDefFoundError: ");
333 utf_fprint_printable_ascii(stderr, name);
338 /* keep compiler happy */
344 java_objectheader *exceptions_new_linkageerror(const char *message,
347 fprintf(stderr, "java.lang.LinkageError: %s",message);
349 utf_fprint_printable_ascii_classname(stderr, c->name);
355 /* keep compiler happy */
360 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
361 utf *name, utf *desc)
363 fprintf(stderr, "java.lang.NoSuchMethodError: ");
364 utf_fprint_printable_ascii(stderr, c->name);
365 fprintf(stderr, ".");
366 utf_fprint_printable_ascii(stderr, name);
367 utf_fprint_printable_ascii(stderr, desc);
372 /* keep compiler happy */
378 java_objectheader *new_internalerror(const char *message, ...)
382 fprintf(stderr, "%s: ", string_java_lang_InternalError);
384 va_start(ap, message);
385 vfprintf(stderr, message, ap);
390 /* keep compiler happy */
396 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
400 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
402 utf_display_printable_ascii(c->name);
403 fprintf(stderr, ": ");
405 va_start(ap, message);
406 vfprintf(stderr, message, ap);
411 /* keep compiler happy */
417 java_objectheader *new_illegalmonitorstateexception(void)
419 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
422 /* keep compiler happy */
428 java_objectheader *new_negativearraysizeexception(void)
430 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
433 /* keep compiler happy */
439 void exceptions_throw_negativearraysizeexception(void)
441 (void) new_negativearraysizeexception();
445 java_objectheader *new_nullpointerexception(void)
447 fprintf(stderr, "%s", string_java_lang_NullPointerException);
450 /* keep compiler happy */
456 void exceptions_throw_nullpointerexception(void)
458 (void) new_nullpointerexception();
462 void classnotfoundexception_to_noclassdeffounderror(void)
466 /* machine dependent stuff ****************************************************/
468 #if defined(ENABLE_THREADS)
469 critical_section_node_t asm_criticalsections;
470 void thread_restartcriticalsection(ucontext_t *uc) {}
473 void md_param_alloc(methoddesc *md) {}
476 #if defined(ENABLE_INTRP)
477 void print_dynamic_super_statistics(void) {}
481 /************************ global variables **********************/
483 chain *ident_chain; /* chain with method and field names in current class */
485 static u4 outputsize;
486 static bool dopadding;
489 static void printIDpart(int c)
491 if ((c >= 'a' && c <= 'z') ||
492 (c >= 'A' && c <= 'Z') ||
493 (c >= '0' && c <= '9') ||
503 char *utf_ptr = u->text;
506 for (i = 0; i < utf_get_number_of_u2s(u); i++)
507 printIDpart(utf_nextu2(&utf_ptr));
511 static void addoutputsize (int len)
514 if (!dopadding) return;
516 newsize = ALIGN(outputsize, len);
518 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
519 outputsize = newsize;
523 void printOverloadPart(utf *desc)
525 char *utf_ptr=desc->text;
530 while ((c = utf_nextu2(&utf_ptr)) != ')') {
540 fprintf(file, "%c", (char) c);
547 while ((c = utf_nextu2(&utf_ptr)) != ';')
554 log_text("invalid method descriptor");
560 static char *printtype(char *utf_ptr)
564 switch (utf_nextu2(&utf_ptr)) {
565 case 'V': fprintf (file, "void");
571 case 'Z': addoutputsize (4);
572 fprintf (file, "s4");
574 case 'J': addoutputsize (8);
575 fprintf (file, "s8");
577 case 'F': addoutputsize (4);
578 fprintf (file, "float");
580 case 'D': addoutputsize (8);
581 fprintf (file, "double");
584 addoutputsize ( sizeof(java_arrayheader*) );
585 switch (utf_nextu2(&utf_ptr)) {
586 case 'I': fprintf (file, "java_intarray*"); break;
587 case 'J': fprintf (file, "java_longarray*"); break;
588 case 'Z': fprintf (file, "java_booleanarray*"); break;
589 case 'B': fprintf (file, "java_bytearray*"); break;
590 case 'S': fprintf (file, "java_shortarray*"); break;
591 case 'C': fprintf (file, "java_chararray*"); break;
592 case 'F': fprintf (file, "java_floatarray*"); break;
593 case 'D': fprintf (file, "java_doublearray*"); break;
595 case '[': fprintf(file, "java_objectarray*");
596 while ((c = utf_nextu2(&utf_ptr)) == '[');
598 while (utf_nextu2(&utf_ptr) != ';');
601 case 'L': fprintf(file, "java_objectarray*");
602 while (utf_nextu2(&utf_ptr) != ';');
605 log_text("invalid type descriptor");
611 addoutputsize ( sizeof(java_objectheader*));
612 fprintf (file, "struct ");
613 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
618 log_text("Unknown type in field descriptor");
626 /***** determine the number of entries of a utf string in the ident chain *****/
628 static int searchidentchain_utf(utf *ident)
630 utf *u = chain_first(ident_chain); /* first element of list */
634 if (u==ident) count++; /* string found */
635 u = chain_next(ident_chain); /* next element in list */
642 /************** print structure for direct access to objects ******************/
644 static void printfields(classinfo *c)
651 addoutputsize(sizeof(java_objectheader));
652 fprintf(file, " java_objectheader header;\n");
656 printfields(c->super.cls);
658 for (i = 0; i < c->fieldscount; i++) {
661 if (!(f->flags & ACC_STATIC)) {
663 printtype(f->descriptor->text);
665 utf_fprint_printable_ascii(file, f->name);
667 /* rename multiple fieldnames */
668 if ((ident_count = searchidentchain_utf(f->name)))
669 fprintf(file, "%d", ident_count - 1);
670 chain_addlast(ident_chain, f->name);
672 fprintf(file, ";\n");
678 /***************** store prototype for native method in file ******************/
680 void printmethod(methodinfo *m)
685 /* search for return-type in descriptor */
686 utf_ptr = m->descriptor->text;
687 while (utf_nextu2(&utf_ptr) != ')');
690 fprintf(file, "\n/*\n * Class: ");
691 utf_fprint_printable_ascii(file, m->class->name);
692 fprintf(file, "\n * Method: ");
693 utf_fprint_printable_ascii(file, m->name);
694 fprintf(file, "\n * Signature: ");
695 utf_fprint_printable_ascii(file, m->descriptor);
696 fprintf(file, "\n */\n");
698 /* create prototype */
699 fprintf(file, "JNIEXPORT ");
701 fprintf(file, " JNICALL Java_");
702 printID(m->class->name);
704 chain_addlast(ident_chain, m->name);
709 /* ATTENTION: We use the methodinfo's isleafmethod variable as
710 nativelyoverloaded, so we can save some space during
714 printOverloadPart(m->descriptor);
716 fprintf(file, "(JNIEnv *env");
718 utf_ptr = m->descriptor->text + 1;
720 if (!(m->flags & ACC_STATIC)) {
721 fprintf(file, ", struct ");
722 printID(m->class->name);
723 fprintf(file, "* this");
726 fprintf(file, ", jclass clazz");
729 if ((*utf_ptr) != ')') fprintf(file, ", ");
731 while ((*utf_ptr) != ')') {
732 utf_ptr = printtype(utf_ptr);
733 fprintf(file, " par%d", paramnum++);
734 if ((*utf_ptr)!=')') fprintf(file, ", ");
737 fprintf(file, ");\n\n");
741 /******* remove package-name in fully-qualified classname *********************/
743 void gen_header_filename(char *buffer, utf *u)
747 for (i = 0; i < utf_get_number_of_u2s(u); i++) {
748 if ((u->text[i] == '/') || (u->text[i] == '$')) {
749 buffer[i] = '_'; /* convert '$' and '/' to '_' */
752 buffer[i] = u->text[i];
755 buffer[utf_get_number_of_u2s(u)] = '\0';
759 /* create headerfile for classes and store native methods in chain ************/
761 void headerfile_generate(classinfo *c, char *opt_directory)
763 char header_filename[1024] = "";
764 char classname[1024];
765 char uclassname[1024];
770 bool nativelyoverloaded;
772 /* prevent compiler warnings */
774 nativelyoverloaded = false;
776 /* open headerfile for class */
777 gen_header_filename(classname, c->name);
779 /* create chain for renaming fields */
780 ident_chain = chain_new();
783 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
786 sprintf(header_filename, "%s.h", classname);
789 file = fopen(header_filename, "w");
791 log_text("Can not open file to store header information");
795 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
797 /* convert to uppercase */
798 for (i = 0; classname[i]; i++) {
799 uclassname[i] = toupper(classname[i]);
801 uclassname[i] = '\0';
803 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
805 /* create structure for direct access to objects */
806 fprintf(file, "/* Structure information for class: ");
807 utf_fprint_printable_ascii(file, c->name);
808 fprintf(file, " */\n\n");
809 fprintf(file, "typedef struct ");
811 fprintf(file, " {\n");
819 fprintf(file, ";\n\n");
821 /* create chain for renaming overloaded methods */
822 chain_free(ident_chain);
823 ident_chain = chain_new();
825 /* create method-prototypes */
827 /* find overloaded methods */
829 for (i = 0; i < c->methodscount; i++) {
830 m = &(c->methods[i]);
832 if (!(m->flags & ACC_NATIVE))
835 /* We use the methodinfo's isleafmethod variable as
836 nativelyoverloaded, so we can save some space during
839 if (!m->isleafmethod) {
840 nativelyoverloaded = false;
842 for (j = i + 1; j < c->methodscount; j++) {
843 m2 = &(c->methods[j]);
845 if (!(m2->flags & ACC_NATIVE))
848 if (m->name == m2->name) {
849 m2->isleafmethod = true;
850 nativelyoverloaded = true;
855 m->isleafmethod = nativelyoverloaded;
858 for (i = 0; i < c->methodscount; i++) {
859 m = &(c->methods[i]);
861 if (m->flags & ACC_NATIVE)
865 chain_free(ident_chain);
867 fprintf(file, "#endif\n\n");
873 /******** print classname, '$' used to seperate inner-class name ***********/
875 void print_classname(classinfo *clazz)
877 utf *u = clazz->name;
878 char *endpos = u->text + u->blength;
879 char *utf_ptr = u->text;
882 while (utf_ptr < endpos) {
883 if ((c = utf_nextu2(&utf_ptr)) == '_')
892 * These are local overrides for various environment variables in Emacs.
893 * Please do not remove this and leave it at the end of the file, where
894 * Emacs will automagically detect them.
895 * ---------------------------------------------------------------------
898 * indent-tabs-mode: t
902 * vim:noexpandtab:sw=4:ts=4: