1 /* src/cacaoh/headers.c - functions for header generation
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 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., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Reinhard Grafl
33 $Id: headers.c 4122 2006-01-10 20:44:14Z 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 *asm_calljavafunction(methodinfo *m, void *arg1, void *arg2,
101 void *arg3, void *arg4) { return NULL; }
103 /* code patching functions */
104 void patcher_builtin_arraycheckcast(u1 *sp) {}
106 #if defined(__MIPS__)
107 long compare_and_swap(long *p, long oldval, long newval)
118 u1 *createcompilerstub(methodinfo *m) { return NULL; }
119 u1 *codegen_createnativestub(functionptr f, methodinfo *m) { return NULL; }
121 void removecompilerstub(u1 *stub) {}
122 void removenativestub(u1 *stub) {}
124 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
125 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
127 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
129 void asm_handle_builtin_exception(classinfo *c) {}
130 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
133 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
138 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
140 return typecheck_TRUE;
143 void typeinfo_init_classinfo(typeinfo *info,classinfo *c)
147 bool typeinfo_init_class(typeinfo *info,classref_or_classinfo c)
152 void typeinfo_print(FILE *file,typeinfo *info,int indent) {}
154 void exceptions_print_exception(java_objectheader *xptr) {}
155 void stacktrace_print_trace(java_objectheader *xptr) {}
158 /* exception functions ********************************************************/
160 /* these should not be called */
162 void throw_main_exception_exit(void) { assert(0); }
163 void throw_exception(void) { assert(0); }
164 void throw_exception_exit(void) { assert(0); }
166 java_objectheader *new_verifyerror(methodinfo *m, const char *message)
170 /* keep compiler happy */
175 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
179 /* keep compiler happy */
185 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
189 fprintf(stderr, "%s: ", exception);
191 va_start(ap, message);
192 vfprintf(stderr, message, ap);
201 void exceptions_throw_outofmemory_exit(void)
203 fprintf(stderr, "java.lang.InternalError: Out of memory\n");
208 java_objectheader *new_exception(const char *classname)
210 fprintf(stderr, "%s\n", classname);
213 /* keep compiler happy */
219 java_objectheader *new_exception_message(const char *classname, const char *message)
221 fprintf(stderr, "%s: %s\n", classname, message);
224 /* keep compiler happy */
230 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
232 fprintf(stderr, "%s: ", classname);
233 utf_display(message);
238 /* keep compiler happy */
244 java_objectheader *new_exception_javastring(const char *classname,
245 java_lang_String *message)
247 fprintf(stderr, "%s: ", classname);
248 /* TODO print message */
253 /* keep compiler happy */
259 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
263 utf_display(c->name);
264 fprintf(stderr, ": ");
266 va_start(ap, message);
267 vfprintf(stderr, message, ap);
274 /* keep compiler happy */
280 void exceptions_throw_classformaterror(classinfo *c, const char *message, ...)
284 va_start(ap, message);
285 (void) new_classformaterror(c, message, ap);
290 java_objectheader *new_classnotfoundexception(utf *name)
292 fprintf(stderr, "java.lang.ClassNotFoundException: ");
293 utf_fprint(stderr, name);
298 /* keep compiler happy */
304 java_objectheader *new_noclassdeffounderror(utf *name)
306 fprintf(stderr, "java.lang.NoClassDefFoundError: ");
307 utf_fprint(stderr, name);
312 /* keep compiler happy */
318 java_objectheader *exceptions_new_linkageerror(const char *message,
321 fprintf(stderr, "java.lang.LinkageError: %s",message);
323 utf_fprint_classname(stderr, c->name);
329 /* keep compiler happy */
334 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
335 utf *name, utf *desc)
337 fprintf(stderr, "java.lang.NoSuchMethodError: ");
338 utf_fprint(stderr, c->name);
339 fprintf(stderr, ".");
340 utf_fprint(stderr, name);
341 utf_fprint(stderr, desc);
346 /* keep compiler happy */
352 java_objectheader *new_internalerror(const char *message, ...)
356 fprintf(stderr, "%s: ", string_java_lang_InternalError);
358 va_start(ap, message);
359 vfprintf(stderr, message, ap);
364 /* keep compiler happy */
370 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
374 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
376 utf_display(c->name);
377 fprintf(stderr, ": ");
379 va_start(ap, message);
380 vfprintf(stderr, message, ap);
385 /* keep compiler happy */
391 java_objectheader *new_illegalmonitorstateexception(void)
393 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
396 /* keep compiler happy */
402 java_objectheader *new_negativearraysizeexception(void)
404 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
407 /* keep compiler happy */
413 void exceptions_throw_negativearraysizeexception(void)
415 (void) new_negativearraysizeexception();
419 java_objectheader *new_nullpointerexception(void)
421 fprintf(stderr, "%s", string_java_lang_NullPointerException);
424 /* keep compiler happy */
430 void exceptions_throw_nullpointerexception(void)
432 (void) new_nullpointerexception();
436 void classnotfoundexception_to_noclassdeffounderror(void)
440 /* machine dependent stuff ****************************************************/
442 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
443 threadcritnode asm_criticalsections;
444 void thread_restartcriticalsection(ucontext_t *uc) {}
447 void md_param_alloc(methoddesc *md) {}
450 /************************ global variables **********************/
452 chain *nativemethod_chain; /* chain with native methods */
453 chain *nativeclass_chain; /* chain with processed classes */
454 chain *ident_chain; /* chain with method and field names in current class */
456 static u4 outputsize;
457 static bool dopadding;
460 static void printIDpart(int c)
462 if ((c >= 'a' && c <= 'z') ||
463 (c >= 'A' && c <= 'Z') ||
464 (c >= '0' && c <= '9') ||
474 char *utf_ptr = u->text;
477 for (i = 0; i < utf_strlen(u); i++)
478 printIDpart(utf_nextu2(&utf_ptr));
482 static void addoutputsize (int len)
485 if (!dopadding) return;
487 newsize = ALIGN(outputsize, len);
489 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
490 outputsize = newsize;
494 void printOverloadPart(utf *desc)
496 char *utf_ptr=desc->text;
501 while ((c = utf_nextu2(&utf_ptr)) != ')') {
511 fprintf(file, "%c", (char) c);
518 while ((c = utf_nextu2(&utf_ptr)) != ';')
525 log_text("invalid method descriptor");
531 static char *printtype(char *utf_ptr)
535 switch (utf_nextu2(&utf_ptr)) {
536 case 'V': fprintf (file, "void");
542 case 'Z': addoutputsize (4);
543 fprintf (file, "s4");
545 case 'J': addoutputsize (8);
546 fprintf (file, "s8");
548 case 'F': addoutputsize (4);
549 fprintf (file, "float");
551 case 'D': addoutputsize (8);
552 fprintf (file, "double");
555 addoutputsize ( sizeof(java_arrayheader*) );
556 switch (utf_nextu2(&utf_ptr)) {
557 case 'I': fprintf (file, "java_intarray*"); break;
558 case 'J': fprintf (file, "java_longarray*"); break;
559 case 'Z': fprintf (file, "java_booleanarray*"); break;
560 case 'B': fprintf (file, "java_bytearray*"); break;
561 case 'S': fprintf (file, "java_shortarray*"); break;
562 case 'C': fprintf (file, "java_chararray*"); break;
563 case 'F': fprintf (file, "java_floatarray*"); break;
564 case 'D': fprintf (file, "java_doublearray*"); break;
566 case '[': fprintf(file, "java_objectarray*");
567 while ((c = utf_nextu2(&utf_ptr)) == '[');
569 while (utf_nextu2(&utf_ptr) != ';');
572 case 'L': fprintf(file, "java_objectarray*");
573 while (utf_nextu2(&utf_ptr) != ';');
576 log_text("invalid type descriptor");
582 addoutputsize ( sizeof(java_objectheader*));
583 fprintf (file, "struct ");
584 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
589 log_text("Unknown type in field descriptor");
597 /***** determine the number of entries of a utf string in the ident chain *****/
599 static int searchidentchain_utf(utf *ident)
601 utf *u = chain_first(ident_chain); /* first element of list */
605 if (u==ident) count++; /* string found */
606 u = chain_next(ident_chain); /* next element in list */
613 /************** print structure for direct access to objects ******************/
615 static void printfields(classinfo *c)
622 addoutputsize(sizeof(java_objectheader));
623 fprintf(file, " java_objectheader header;\n");
627 printfields(c->super.cls);
629 for (i = 0; i < c->fieldscount; i++) {
632 if (!(f->flags & ACC_STATIC)) {
634 printtype(f->descriptor->text);
636 utf_fprint(file, f->name);
638 /* rename multiple fieldnames */
639 if ((ident_count = searchidentchain_utf(f->name)))
640 fprintf(file, "%d", ident_count - 1);
641 chain_addlast(ident_chain, f->name);
643 fprintf(file, ";\n");
649 /***************** store prototype for native method in file ******************/
651 void printmethod(methodinfo *m)
656 /* search for return-type in descriptor */
657 utf_ptr = m->descriptor->text;
658 while (utf_nextu2(&utf_ptr) != ')');
661 fprintf(file, "\n/*\n * Class: ");
662 utf_fprint(file, m->class->name);
663 fprintf(file, "\n * Method: ");
664 utf_fprint(file, m->name);
665 fprintf(file, "\n * Signature: ");
666 utf_fprint(file, m->descriptor);
667 fprintf(file, "\n */\n");
669 /* create prototype */
670 fprintf(file, "JNIEXPORT ");
672 fprintf(file, " JNICALL Java_");
673 printID(m->class->name);
675 chain_addlast(ident_chain, m->name);
679 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
680 fprintf(file, "(JNIEnv *env");
682 utf_ptr = m->descriptor->text + 1;
684 if (!(m->flags & ACC_STATIC)) {
685 fprintf(file, ", struct ");
686 printID(m->class->name);
687 fprintf(file, "* this");
690 fprintf(file, ", jclass clazz");
693 if ((*utf_ptr) != ')') fprintf(file, ", ");
695 while ((*utf_ptr) != ')') {
696 utf_ptr = printtype(utf_ptr);
697 fprintf(file, " par%d", paramnum++);
698 if ((*utf_ptr)!=')') fprintf(file, ", ");
701 fprintf(file, ");\n\n");
705 /******* remove package-name in fully-qualified classname *********************/
707 void gen_header_filename(char *buffer, utf *u)
711 for (i = 0; i < utf_strlen(u); i++) {
712 if ((u->text[i] == '/') || (u->text[i] == '$')) {
713 buffer[i] = '_'; /* convert '$' and '/' to '_' */
716 buffer[i] = u->text[i];
719 buffer[utf_strlen(u)] = '\0';
723 /* create headerfile for classes and store native methods in chain ************/
725 void headerfile_generate(classinfo *c, char *opt_directory)
727 char header_filename[1024] = "";
728 char classname[1024];
729 char uclassname[1024];
734 u2 nativelyoverloaded;
736 /* store class in chain */
737 chain_addlast(nativeclass_chain, c);
739 /* open headerfile for class */
740 gen_header_filename(classname, c->name);
742 /* create chain for renaming fields */
743 ident_chain = chain_new();
746 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
749 sprintf(header_filename, "%s.h", classname);
752 file = fopen(header_filename, "w");
754 log_text("Can not open file to store header information");
758 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
760 /* convert to uppercase */
761 for (i = 0; classname[i]; i++) {
762 uclassname[i] = toupper(classname[i]);
764 uclassname[i] = '\0';
766 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
768 /* create structure for direct access to objects */
769 fprintf(file, "/* Structure information for class: ");
770 utf_fprint(file, c->name);
771 fprintf(file, " */\n\n");
772 fprintf(file, "typedef struct ");
774 fprintf(file, " {\n");
782 fprintf(file, ";\n\n");
784 /* create chain for renaming overloaded methods */
785 chain_free(ident_chain);
786 ident_chain = chain_new();
788 /* create method-prototypes */
790 /* find overloaded methods */
791 for (i = 0; i < c->methodscount; i++) {
793 m = &(c->methods[i]);
795 if (!(m->flags & ACC_NATIVE)) continue;
796 if (!m->nativelyoverloaded) {
797 nativelyoverloaded=false;
798 for (i2=i+1;i2<c->methodscount; i2++) {
799 m2 = &(c->methods[i2]);
800 if (!(m2->flags & ACC_NATIVE)) continue;
801 if (m->name==m2->name) {
802 m2->nativelyoverloaded=true;
803 nativelyoverloaded=true;
806 m->nativelyoverloaded=nativelyoverloaded;
811 for (i = 0; i < c->methodscount; i++) {
813 m = &(c->methods[i]);
815 if (m->flags & ACC_NATIVE) {
816 chain_addlast(nativemethod_chain, m);
821 chain_free(ident_chain);
823 fprintf(file, "#endif\n\n");
829 /******** print classname, '$' used to seperate inner-class name ***********/
831 void print_classname(classinfo *clazz)
833 utf *u = clazz->name;
834 char *endpos = u->text + u->blength;
835 char *utf_ptr = u->text;
838 while (utf_ptr < endpos) {
839 if ((c = utf_nextu2(&utf_ptr)) == '_')
848 * These are local overrides for various environment variables in Emacs.
849 * Please do not remove this and leave it at the end of the file, where
850 * Emacs will automagically detect them.
851 * ---------------------------------------------------------------------
854 * indent-tabs-mode: t