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 4552 2006-03-04 17:15: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 */
73 u1 intrp_main_stack[1];
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_intern(methodinfo *m, void *a0, void *a1,
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 u1 *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) {}
172 /* exception functions ********************************************************/
174 /* these should not be called */
176 void throw_main_exception_exit(void) { assert(0); }
177 void throw_exception(void) { assert(0); }
178 void throw_exception_exit(void) { assert(0); }
180 java_objectheader *new_verifyerror(methodinfo *m, const char *message)
184 /* keep compiler happy */
189 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
193 /* keep compiler happy */
199 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
203 fprintf(stderr, "%s: ", exception);
205 va_start(ap, message);
206 vfprintf(stderr, message, ap);
215 void exceptions_throw_outofmemory_exit(void)
217 fprintf(stderr, "java.lang.InternalError: Out of memory\n");
222 java_objectheader *new_exception(const char *classname)
224 fprintf(stderr, "%s\n", classname);
227 /* keep compiler happy */
233 java_objectheader *new_exception_message(const char *classname, const char *message)
235 fprintf(stderr, "%s: %s\n", classname, message);
238 /* keep compiler happy */
244 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
246 fprintf(stderr, "%s: ", classname);
247 utf_display(message);
252 /* keep compiler happy */
258 java_objectheader *new_exception_javastring(const char *classname,
259 java_lang_String *message)
261 fprintf(stderr, "%s: ", classname);
262 /* TODO print message */
267 /* keep compiler happy */
273 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
277 utf_display(c->name);
278 fprintf(stderr, ": ");
280 va_start(ap, message);
281 vfprintf(stderr, message, ap);
288 /* keep compiler happy */
294 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
298 va_start(ap, message);
299 (void) new_classformaterror(c, message, ap);
304 java_objectheader *new_classnotfoundexception(utf *name)
306 fprintf(stderr, "java.lang.ClassNotFoundException: ");
307 utf_fprint(stderr, name);
312 /* keep compiler happy */
318 java_objectheader *new_noclassdeffounderror(utf *name)
320 fprintf(stderr, "java.lang.NoClassDefFoundError: ");
321 utf_fprint(stderr, name);
326 /* keep compiler happy */
332 java_objectheader *exceptions_new_linkageerror(const char *message,
335 fprintf(stderr, "java.lang.LinkageError: %s",message);
337 utf_fprint_classname(stderr, c->name);
343 /* keep compiler happy */
348 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
349 utf *name, utf *desc)
351 fprintf(stderr, "java.lang.NoSuchMethodError: ");
352 utf_fprint(stderr, c->name);
353 fprintf(stderr, ".");
354 utf_fprint(stderr, name);
355 utf_fprint(stderr, desc);
360 /* keep compiler happy */
366 java_objectheader *new_internalerror(const char *message, ...)
370 fprintf(stderr, "%s: ", string_java_lang_InternalError);
372 va_start(ap, message);
373 vfprintf(stderr, message, ap);
378 /* keep compiler happy */
384 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
388 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
390 utf_display(c->name);
391 fprintf(stderr, ": ");
393 va_start(ap, message);
394 vfprintf(stderr, message, ap);
399 /* keep compiler happy */
405 java_objectheader *new_illegalmonitorstateexception(void)
407 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
410 /* keep compiler happy */
416 java_objectheader *new_negativearraysizeexception(void)
418 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
421 /* keep compiler happy */
427 void exceptions_throw_negativearraysizeexception(void)
429 (void) new_negativearraysizeexception();
433 java_objectheader *new_nullpointerexception(void)
435 fprintf(stderr, "%s", string_java_lang_NullPointerException);
438 /* keep compiler happy */
444 void exceptions_throw_nullpointerexception(void)
446 (void) new_nullpointerexception();
450 void classnotfoundexception_to_noclassdeffounderror(void)
454 /* machine dependent stuff ****************************************************/
456 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
457 threadcritnode asm_criticalsections;
458 void thread_restartcriticalsection(ucontext_t *uc) {}
461 void md_param_alloc(methoddesc *md) {}
464 #if defined(ENABLE_INTRP)
465 void print_dynamic_super_statistics(void) {}
469 /************************ global variables **********************/
471 chain *ident_chain; /* chain with method and field names in current class */
473 static u4 outputsize;
474 static bool dopadding;
477 static void printIDpart(int c)
479 if ((c >= 'a' && c <= 'z') ||
480 (c >= 'A' && c <= 'Z') ||
481 (c >= '0' && c <= '9') ||
491 char *utf_ptr = u->text;
494 for (i = 0; i < utf_strlen(u); i++)
495 printIDpart(utf_nextu2(&utf_ptr));
499 static void addoutputsize (int len)
502 if (!dopadding) return;
504 newsize = ALIGN(outputsize, len);
506 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
507 outputsize = newsize;
511 void printOverloadPart(utf *desc)
513 char *utf_ptr=desc->text;
518 while ((c = utf_nextu2(&utf_ptr)) != ')') {
528 fprintf(file, "%c", (char) c);
535 while ((c = utf_nextu2(&utf_ptr)) != ';')
542 log_text("invalid method descriptor");
548 static char *printtype(char *utf_ptr)
552 switch (utf_nextu2(&utf_ptr)) {
553 case 'V': fprintf (file, "void");
559 case 'Z': addoutputsize (4);
560 fprintf (file, "s4");
562 case 'J': addoutputsize (8);
563 fprintf (file, "s8");
565 case 'F': addoutputsize (4);
566 fprintf (file, "float");
568 case 'D': addoutputsize (8);
569 fprintf (file, "double");
572 addoutputsize ( sizeof(java_arrayheader*) );
573 switch (utf_nextu2(&utf_ptr)) {
574 case 'I': fprintf (file, "java_intarray*"); break;
575 case 'J': fprintf (file, "java_longarray*"); break;
576 case 'Z': fprintf (file, "java_booleanarray*"); break;
577 case 'B': fprintf (file, "java_bytearray*"); break;
578 case 'S': fprintf (file, "java_shortarray*"); break;
579 case 'C': fprintf (file, "java_chararray*"); break;
580 case 'F': fprintf (file, "java_floatarray*"); break;
581 case 'D': fprintf (file, "java_doublearray*"); break;
583 case '[': fprintf(file, "java_objectarray*");
584 while ((c = utf_nextu2(&utf_ptr)) == '[');
586 while (utf_nextu2(&utf_ptr) != ';');
589 case 'L': fprintf(file, "java_objectarray*");
590 while (utf_nextu2(&utf_ptr) != ';');
593 log_text("invalid type descriptor");
599 addoutputsize ( sizeof(java_objectheader*));
600 fprintf (file, "struct ");
601 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
606 log_text("Unknown type in field descriptor");
614 /***** determine the number of entries of a utf string in the ident chain *****/
616 static int searchidentchain_utf(utf *ident)
618 utf *u = chain_first(ident_chain); /* first element of list */
622 if (u==ident) count++; /* string found */
623 u = chain_next(ident_chain); /* next element in list */
630 /************** print structure for direct access to objects ******************/
632 static void printfields(classinfo *c)
639 addoutputsize(sizeof(java_objectheader));
640 fprintf(file, " java_objectheader header;\n");
644 printfields(c->super.cls);
646 for (i = 0; i < c->fieldscount; i++) {
649 if (!(f->flags & ACC_STATIC)) {
651 printtype(f->descriptor->text);
653 utf_fprint(file, f->name);
655 /* rename multiple fieldnames */
656 if ((ident_count = searchidentchain_utf(f->name)))
657 fprintf(file, "%d", ident_count - 1);
658 chain_addlast(ident_chain, f->name);
660 fprintf(file, ";\n");
666 /***************** store prototype for native method in file ******************/
668 void printmethod(methodinfo *m)
673 /* search for return-type in descriptor */
674 utf_ptr = m->descriptor->text;
675 while (utf_nextu2(&utf_ptr) != ')');
678 fprintf(file, "\n/*\n * Class: ");
679 utf_fprint(file, m->class->name);
680 fprintf(file, "\n * Method: ");
681 utf_fprint(file, m->name);
682 fprintf(file, "\n * Signature: ");
683 utf_fprint(file, m->descriptor);
684 fprintf(file, "\n */\n");
686 /* create prototype */
687 fprintf(file, "JNIEXPORT ");
689 fprintf(file, " JNICALL Java_");
690 printID(m->class->name);
692 chain_addlast(ident_chain, m->name);
697 /* ATTENTION: We use the methodinfo's isleafmethod variable as
698 nativelyoverloaded, so we can save some space during
702 printOverloadPart(m->descriptor);
704 fprintf(file, "(JNIEnv *env");
706 utf_ptr = m->descriptor->text + 1;
708 if (!(m->flags & ACC_STATIC)) {
709 fprintf(file, ", struct ");
710 printID(m->class->name);
711 fprintf(file, "* this");
714 fprintf(file, ", jclass clazz");
717 if ((*utf_ptr) != ')') fprintf(file, ", ");
719 while ((*utf_ptr) != ')') {
720 utf_ptr = printtype(utf_ptr);
721 fprintf(file, " par%d", paramnum++);
722 if ((*utf_ptr)!=')') fprintf(file, ", ");
725 fprintf(file, ");\n\n");
729 /******* remove package-name in fully-qualified classname *********************/
731 void gen_header_filename(char *buffer, utf *u)
735 for (i = 0; i < utf_strlen(u); i++) {
736 if ((u->text[i] == '/') || (u->text[i] == '$')) {
737 buffer[i] = '_'; /* convert '$' and '/' to '_' */
740 buffer[i] = u->text[i];
743 buffer[utf_strlen(u)] = '\0';
747 /* create headerfile for classes and store native methods in chain ************/
749 void headerfile_generate(classinfo *c, char *opt_directory)
751 char header_filename[1024] = "";
752 char classname[1024];
753 char uclassname[1024];
758 bool nativelyoverloaded;
760 /* prevent compiler warnings */
762 nativelyoverloaded = false;
764 /* open headerfile for class */
765 gen_header_filename(classname, c->name);
767 /* create chain for renaming fields */
768 ident_chain = chain_new();
771 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
774 sprintf(header_filename, "%s.h", classname);
777 file = fopen(header_filename, "w");
779 log_text("Can not open file to store header information");
783 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
785 /* convert to uppercase */
786 for (i = 0; classname[i]; i++) {
787 uclassname[i] = toupper(classname[i]);
789 uclassname[i] = '\0';
791 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
793 /* create structure for direct access to objects */
794 fprintf(file, "/* Structure information for class: ");
795 utf_fprint(file, c->name);
796 fprintf(file, " */\n\n");
797 fprintf(file, "typedef struct ");
799 fprintf(file, " {\n");
807 fprintf(file, ";\n\n");
809 /* create chain for renaming overloaded methods */
810 chain_free(ident_chain);
811 ident_chain = chain_new();
813 /* create method-prototypes */
815 /* find overloaded methods */
817 for (i = 0; i < c->methodscount; i++) {
818 m = &(c->methods[i]);
820 if (!(m->flags & ACC_NATIVE))
823 /* We use the methodinfo's isleafmethod variable as
824 nativelyoverloaded, so we can save some space during
827 if (!m->isleafmethod) {
828 nativelyoverloaded = false;
830 for (j = i + 1; j < c->methodscount; j++) {
831 m2 = &(c->methods[j]);
833 if (!(m2->flags & ACC_NATIVE))
836 if (m->name == m2->name) {
837 m2->isleafmethod = true;
838 nativelyoverloaded = true;
843 m->isleafmethod = nativelyoverloaded;
846 for (i = 0; i < c->methodscount; i++) {
847 m = &(c->methods[i]);
849 if (m->flags & ACC_NATIVE)
853 chain_free(ident_chain);
855 fprintf(file, "#endif\n\n");
861 /******** print classname, '$' used to seperate inner-class name ***********/
863 void print_classname(classinfo *clazz)
865 utf *u = clazz->name;
866 char *endpos = u->text + u->blength;
867 char *utf_ptr = u->text;
870 while (utf_ptr < endpos) {
871 if ((c = utf_nextu2(&utf_ptr)) == '_')
880 * These are local overrides for various environment variables in Emacs.
881 * Please do not remove this and leave it at the end of the file, where
882 * Emacs will automagically detect them.
883 * ---------------------------------------------------------------------
886 * indent-tabs-mode: t