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 3641 2005-11-08 17:27:29Z twisti $
46 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
47 # if defined(__DARWIN__)
50 # include <ucontext.h>
54 #include "mm/memory.h"
55 #include "native/include/java_lang_String.h"
56 #include "native/include/java_lang_Throwable.h"
57 #include "toolbox/chain.h"
58 #include "toolbox/logging.h"
59 #include "vm/builtin.h"
61 #include "vm/global.h"
62 #include "vm/method.h"
63 #include "vm/tables.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 void asm_call_jit_compiler(void) {}
105 /* code patching functions */
106 void patcher_builtin_new(u1 *sp) {}
107 void patcher_builtin_newarray(u1 *sp) {}
108 void patcher_builtin_arraycheckcast(u1 *sp) {}
109 void patcher_builtin_arrayinstanceof(u1 *sp) {}
111 s4 asm_builtin_idiv(s4 a, s4 b) { return 0; }
112 s4 asm_builtin_irem(s4 a, s4 b) { return 0; }
113 s8 asm_builtin_ldiv(s8 a, s8 b) { return 0; }
114 s8 asm_builtin_lrem(s8 a, s8 b) { return 0; }
116 s4 asm_builtin_f2i(float a) { return 0; }
117 s8 asm_builtin_f2l(float a) { return 0; }
118 s4 asm_builtin_d2i(double a) { return 0; }
119 s8 asm_builtin_d2l(double a) { return 0; }
121 void use_class_as_object(void) {}
122 void asm_builtin_monitorenter(java_objectheader *o) {}
123 void *asm_builtin_monitorexit(java_objectheader *o) { return NULL; }
125 s4 asm_builtin_arraycheckcast(java_objectheader *obj, vftbl_t *target)
130 #if defined(__MIPS__)
131 long compare_and_swap(long *p, long oldval, long newval)
142 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
144 functionptr createcompilerstub(methodinfo *m) { return NULL; }
145 functionptr codegen_createnativestub(functionptr f, methodinfo *m) { return NULL; }
147 void removecompilerstub(functionptr stub) {}
148 void removenativestub(functionptr stub) {}
150 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
151 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
153 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
155 void asm_handle_builtin_exception(classinfo *c) {}
156 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
159 void setVMClassField(classinfo *c) {}
160 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
165 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
167 return typecheck_TRUE;
170 bool typeinfo_init_class(typeinfo *info,classref_or_classinfo c)
175 void typeinfo_print(FILE *file,typeinfo *info,int indent) {}
177 void stacktrace_print_trace(stackTraceBuffer *stb) {}
180 /* exception functions ********************************************************/
182 /* these should not be called */
184 void throw_exception(void) { assert(0); }
185 void throw_exception_exit(void) { assert(0); }
187 java_objectheader *new_verifyerror(methodinfo *m, const char *message)
191 /* keep compiler happy */
196 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
200 /* keep compiler happy */
206 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
210 fprintf(stderr, "%s: ", exception);
212 va_start(ap, message);
213 vfprintf(stderr, message, ap);
222 void exceptions_throw_outofmemory_exit(void)
224 fprintf(stderr, "java.lang.InternalError: Out of memory\n");
229 java_objectheader *new_exception(const char *classname)
231 fprintf(stderr, "%s\n", classname);
234 /* keep compiler happy */
240 java_objectheader *new_exception_message(const char *classname, const char *message)
242 fprintf(stderr, "%s: %s\n", classname, message);
245 /* keep compiler happy */
251 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
253 fprintf(stderr, "%s: ", classname);
254 utf_display(message);
259 /* keep compiler happy */
265 java_objectheader *new_exception_javastring(const char *classname,
266 java_lang_String *message)
268 fprintf(stderr, "%s: ", classname);
269 /* TODO print message */
274 /* keep compiler happy */
280 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
284 utf_display(c->name);
285 fprintf(stderr, ": ");
287 va_start(ap, message);
288 vfprintf(stderr, message, ap);
295 /* keep compiler happy */
301 java_objectheader *new_classnotfoundexception(utf *name)
303 fprintf(stderr, "java.lang.ClassNotFoundException: ");
304 utf_fprint(stderr, name);
309 /* keep compiler happy */
315 java_objectheader *new_noclassdeffounderror(utf *name)
317 fprintf(stderr, "java.lang.NoClassDefFoundError: ");
318 utf_fprint(stderr, name);
323 /* keep compiler happy */
329 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
330 utf *name, utf *desc)
332 fprintf(stderr, "java.lang.NoSuchMethodError: ");
333 utf_fprint(stderr, c->name);
334 fprintf(stderr, ".");
335 utf_fprint(stderr, name);
336 utf_fprint(stderr, desc);
341 /* keep compiler happy */
347 java_objectheader *new_internalerror(const char *message, ...)
351 fprintf(stderr, "%s: ", string_java_lang_InternalError);
353 va_start(ap, message);
354 vfprintf(stderr, message, ap);
359 /* keep compiler happy */
365 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
369 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
371 utf_display(c->name);
372 fprintf(stderr, ": ");
374 va_start(ap, message);
375 vfprintf(stderr, message, ap);
380 /* keep compiler happy */
386 java_objectheader *new_illegalmonitorstateexception(void)
388 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
391 /* keep compiler happy */
397 java_objectheader *new_negativearraysizeexception(void)
399 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
402 /* keep compiler happy */
408 java_objectheader *new_nullpointerexception(void)
410 fprintf(stderr, "%s", string_java_lang_NullPointerException);
413 /* keep compiler happy */
418 void classnotfoundexception_to_noclassdeffounderror(void)
422 /* machine dependent stuff ****************************************************/
424 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
425 threadcritnode asm_criticalsections;
426 void thread_restartcriticalsection(ucontext_t *uc) {}
429 void md_param_alloc(methoddesc *md) {}
432 /************************ global variables **********************/
434 chain *nativemethod_chain; /* chain with native methods */
435 chain *nativeclass_chain; /* chain with processed classes */
436 chain *ident_chain; /* chain with method and field names in current class */
438 static u4 outputsize;
439 static bool dopadding;
442 static void printIDpart(int c)
444 if ((c >= 'a' && c <= 'z') ||
445 (c >= 'A' && c <= 'Z') ||
446 (c >= '0' && c <= '9') ||
456 char *utf_ptr = u->text;
459 for (i = 0; i < utf_strlen(u); i++)
460 printIDpart(utf_nextu2(&utf_ptr));
464 static void addoutputsize (int len)
467 if (!dopadding) return;
469 newsize = ALIGN(outputsize, len);
471 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
472 outputsize = newsize;
476 void printOverloadPart(utf *desc)
478 char *utf_ptr=desc->text;
483 while ((c = utf_nextu2(&utf_ptr)) != ')') {
493 fprintf(file, "%c", (char) c);
500 while ((c = utf_nextu2(&utf_ptr)) != ';')
507 log_text("invalid method descriptor");
513 static char *printtype(char *utf_ptr)
517 switch (utf_nextu2(&utf_ptr)) {
518 case 'V': fprintf (file, "void");
524 case 'Z': addoutputsize (4);
525 fprintf (file, "s4");
527 case 'J': addoutputsize (8);
528 fprintf (file, "s8");
530 case 'F': addoutputsize (4);
531 fprintf (file, "float");
533 case 'D': addoutputsize (8);
534 fprintf (file, "double");
537 addoutputsize ( sizeof(java_arrayheader*) );
538 switch (utf_nextu2(&utf_ptr)) {
539 case 'I': fprintf (file, "java_intarray*"); break;
540 case 'J': fprintf (file, "java_longarray*"); break;
541 case 'Z': fprintf (file, "java_booleanarray*"); break;
542 case 'B': fprintf (file, "java_bytearray*"); break;
543 case 'S': fprintf (file, "java_shortarray*"); break;
544 case 'C': fprintf (file, "java_chararray*"); break;
545 case 'F': fprintf (file, "java_floatarray*"); break;
546 case 'D': fprintf (file, "java_doublearray*"); break;
548 case '[': fprintf(file, "java_objectarray*");
549 while ((c = utf_nextu2(&utf_ptr)) == '[');
551 while (utf_nextu2(&utf_ptr) != ';');
554 case 'L': fprintf(file, "java_objectarray*");
555 while (utf_nextu2(&utf_ptr) != ';');
558 log_text("invalid type descriptor");
564 addoutputsize ( sizeof(java_objectheader*));
565 fprintf (file, "struct ");
566 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
571 log_text("Unknown type in field descriptor");
579 /***** determine the number of entries of a utf string in the ident chain *****/
581 static int searchidentchain_utf(utf *ident)
583 utf *u = chain_first(ident_chain); /* first element of list */
587 if (u==ident) count++; /* string found */
588 u = chain_next(ident_chain); /* next element in list */
595 /************** print structure for direct access to objects ******************/
597 static void printfields(classinfo *c)
604 addoutputsize(sizeof(java_objectheader));
605 fprintf(file, " java_objectheader header;\n");
609 printfields(c->super.cls);
611 for (i = 0; i < c->fieldscount; i++) {
614 if (!(f->flags & ACC_STATIC)) {
616 printtype(f->descriptor->text);
618 utf_fprint(file, f->name);
620 /* rename multiple fieldnames */
621 if ((ident_count = searchidentchain_utf(f->name)))
622 fprintf(file, "%d", ident_count - 1);
623 chain_addlast(ident_chain, f->name);
625 fprintf(file, ";\n");
631 /***************** store prototype for native method in file ******************/
633 void printmethod(methodinfo *m)
638 /* search for return-type in descriptor */
639 utf_ptr = m->descriptor->text;
640 while (utf_nextu2(&utf_ptr) != ')');
643 fprintf(file, "\n/*\n * Class: ");
644 utf_fprint(file, m->class->name);
645 fprintf(file, "\n * Method: ");
646 utf_fprint(file, m->name);
647 fprintf(file, "\n * Signature: ");
648 utf_fprint(file, m->descriptor);
649 fprintf(file, "\n */\n");
651 /* create prototype */
652 fprintf(file, "JNIEXPORT ");
654 fprintf(file, " JNICALL Java_");
655 printID(m->class->name);
657 chain_addlast(ident_chain, m->name);
661 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
662 fprintf(file, "(JNIEnv *env");
664 utf_ptr = m->descriptor->text + 1;
666 if (!(m->flags & ACC_STATIC)) {
667 fprintf(file, ", struct ");
668 printID(m->class->name);
669 fprintf(file, "* this");
672 fprintf(file, ", jclass clazz");
675 if ((*utf_ptr) != ')') fprintf(file, ", ");
677 while ((*utf_ptr) != ')') {
678 utf_ptr = printtype(utf_ptr);
679 fprintf(file, " par%d", paramnum++);
680 if ((*utf_ptr)!=')') fprintf(file, ", ");
683 fprintf(file, ");\n\n");
687 /******* remove package-name in fully-qualified classname *********************/
689 void gen_header_filename(char *buffer, utf *u)
693 for (i = 0; i < utf_strlen(u); i++) {
694 if ((u->text[i] == '/') || (u->text[i] == '$')) {
695 buffer[i] = '_'; /* convert '$' and '/' to '_' */
698 buffer[i] = u->text[i];
701 buffer[utf_strlen(u)] = '\0';
705 /* create headerfile for classes and store native methods in chain ************/
707 void headerfile_generate(classinfo *c, char *opt_directory)
709 char header_filename[1024] = "";
710 char classname[1024];
711 char uclassname[1024];
716 u2 nativelyoverloaded;
718 /* store class in chain */
719 chain_addlast(nativeclass_chain, c);
721 /* open headerfile for class */
722 gen_header_filename(classname, c->name);
724 /* create chain for renaming fields */
725 ident_chain = chain_new();
728 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
731 sprintf(header_filename, "%s.h", classname);
734 file = fopen(header_filename, "w");
736 log_text("Can not open file to store header information");
740 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
742 /* convert to uppercase */
743 for (i = 0; classname[i]; i++) {
744 uclassname[i] = toupper(classname[i]);
746 uclassname[i] = '\0';
748 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
750 /* create structure for direct access to objects */
751 fprintf(file, "/* Structure information for class: ");
752 utf_fprint(file, c->name);
753 fprintf(file, " */\n\n");
754 fprintf(file, "typedef struct ");
756 fprintf(file, " {\n");
764 fprintf(file, ";\n\n");
766 /* create chain for renaming overloaded methods */
767 chain_free(ident_chain);
768 ident_chain = chain_new();
770 /* create method-prototypes */
772 /* find overloaded methods */
773 for (i = 0; i < c->methodscount; i++) {
775 m = &(c->methods[i]);
777 if (!(m->flags & ACC_NATIVE)) continue;
778 if (!m->nativelyoverloaded) {
779 nativelyoverloaded=false;
780 for (i2=i+1;i2<c->methodscount; i2++) {
781 m2 = &(c->methods[i2]);
782 if (!(m2->flags & ACC_NATIVE)) continue;
783 if (m->name==m2->name) {
784 m2->nativelyoverloaded=true;
785 nativelyoverloaded=true;
788 m->nativelyoverloaded=nativelyoverloaded;
793 for (i = 0; i < c->methodscount; i++) {
795 m = &(c->methods[i]);
797 if (m->flags & ACC_NATIVE) {
798 chain_addlast(nativemethod_chain, m);
803 chain_free(ident_chain);
805 fprintf(file, "#endif\n\n");
811 /******** print classname, '$' used to seperate inner-class name ***********/
813 void print_classname(classinfo *clazz)
815 utf *u = clazz->name;
816 char *endpos = u->text + u->blength;
817 char *utf_ptr = u->text;
820 while (utf_ptr < endpos) {
821 if ((c = utf_nextu2(&utf_ptr)) == '_')
830 * These are local overrides for various environment variables in Emacs.
831 * Please do not remove this and leave it at the end of the file, where
832 * Emacs will automagically detect them.
833 * ---------------------------------------------------------------------
836 * indent-tabs-mode: t