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 3673 2005-11-16 12:03:38Z 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 void typeinfo_init_classinfo(typeinfo *info,classinfo *c)
174 bool typeinfo_init_class(typeinfo *info,classref_or_classinfo c)
179 void typeinfo_print(FILE *file,typeinfo *info,int indent) {}
181 void exceptions_print_exception(java_objectheader *xptr) {}
182 void stacktrace_print_trace(java_objectheader *xptr) {}
185 /* exception functions ********************************************************/
187 /* these should not be called */
189 void throw_main_exception_exit(void) { assert(0); }
190 void throw_exception(void) { assert(0); }
191 void throw_exception_exit(void) { assert(0); }
193 java_objectheader *new_verifyerror(methodinfo *m, const char *message)
197 /* keep compiler happy */
202 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
206 /* keep compiler happy */
212 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
216 fprintf(stderr, "%s: ", exception);
218 va_start(ap, message);
219 vfprintf(stderr, message, ap);
228 void exceptions_throw_outofmemory_exit(void)
230 fprintf(stderr, "java.lang.InternalError: Out of memory\n");
235 java_objectheader *new_exception(const char *classname)
237 fprintf(stderr, "%s\n", classname);
240 /* keep compiler happy */
246 java_objectheader *new_exception_message(const char *classname, const char *message)
248 fprintf(stderr, "%s: %s\n", classname, message);
251 /* keep compiler happy */
257 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
259 fprintf(stderr, "%s: ", classname);
260 utf_display(message);
265 /* keep compiler happy */
271 java_objectheader *new_exception_javastring(const char *classname,
272 java_lang_String *message)
274 fprintf(stderr, "%s: ", classname);
275 /* TODO print message */
280 /* keep compiler happy */
286 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
290 utf_display(c->name);
291 fprintf(stderr, ": ");
293 va_start(ap, message);
294 vfprintf(stderr, message, ap);
301 /* keep compiler happy */
307 java_objectheader *new_classnotfoundexception(utf *name)
309 fprintf(stderr, "java.lang.ClassNotFoundException: ");
310 utf_fprint(stderr, name);
315 /* keep compiler happy */
321 java_objectheader *new_noclassdeffounderror(utf *name)
323 fprintf(stderr, "java.lang.NoClassDefFoundError: ");
324 utf_fprint(stderr, name);
329 /* keep compiler happy */
335 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
336 utf *name, utf *desc)
338 fprintf(stderr, "java.lang.NoSuchMethodError: ");
339 utf_fprint(stderr, c->name);
340 fprintf(stderr, ".");
341 utf_fprint(stderr, name);
342 utf_fprint(stderr, desc);
347 /* keep compiler happy */
353 java_objectheader *new_internalerror(const char *message, ...)
357 fprintf(stderr, "%s: ", string_java_lang_InternalError);
359 va_start(ap, message);
360 vfprintf(stderr, message, ap);
365 /* keep compiler happy */
371 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
375 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
377 utf_display(c->name);
378 fprintf(stderr, ": ");
380 va_start(ap, message);
381 vfprintf(stderr, message, ap);
386 /* keep compiler happy */
392 java_objectheader *new_illegalmonitorstateexception(void)
394 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
397 /* keep compiler happy */
403 java_objectheader *new_negativearraysizeexception(void)
405 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
408 /* keep compiler happy */
414 java_objectheader *new_nullpointerexception(void)
416 fprintf(stderr, "%s", string_java_lang_NullPointerException);
419 /* keep compiler happy */
424 void classnotfoundexception_to_noclassdeffounderror(void)
428 /* machine dependent stuff ****************************************************/
430 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
431 threadcritnode asm_criticalsections;
432 void thread_restartcriticalsection(ucontext_t *uc) {}
435 void md_param_alloc(methoddesc *md) {}
438 /************************ global variables **********************/
440 chain *nativemethod_chain; /* chain with native methods */
441 chain *nativeclass_chain; /* chain with processed classes */
442 chain *ident_chain; /* chain with method and field names in current class */
444 static u4 outputsize;
445 static bool dopadding;
448 static void printIDpart(int c)
450 if ((c >= 'a' && c <= 'z') ||
451 (c >= 'A' && c <= 'Z') ||
452 (c >= '0' && c <= '9') ||
462 char *utf_ptr = u->text;
465 for (i = 0; i < utf_strlen(u); i++)
466 printIDpart(utf_nextu2(&utf_ptr));
470 static void addoutputsize (int len)
473 if (!dopadding) return;
475 newsize = ALIGN(outputsize, len);
477 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
478 outputsize = newsize;
482 void printOverloadPart(utf *desc)
484 char *utf_ptr=desc->text;
489 while ((c = utf_nextu2(&utf_ptr)) != ')') {
499 fprintf(file, "%c", (char) c);
506 while ((c = utf_nextu2(&utf_ptr)) != ';')
513 log_text("invalid method descriptor");
519 static char *printtype(char *utf_ptr)
523 switch (utf_nextu2(&utf_ptr)) {
524 case 'V': fprintf (file, "void");
530 case 'Z': addoutputsize (4);
531 fprintf (file, "s4");
533 case 'J': addoutputsize (8);
534 fprintf (file, "s8");
536 case 'F': addoutputsize (4);
537 fprintf (file, "float");
539 case 'D': addoutputsize (8);
540 fprintf (file, "double");
543 addoutputsize ( sizeof(java_arrayheader*) );
544 switch (utf_nextu2(&utf_ptr)) {
545 case 'I': fprintf (file, "java_intarray*"); break;
546 case 'J': fprintf (file, "java_longarray*"); break;
547 case 'Z': fprintf (file, "java_booleanarray*"); break;
548 case 'B': fprintf (file, "java_bytearray*"); break;
549 case 'S': fprintf (file, "java_shortarray*"); break;
550 case 'C': fprintf (file, "java_chararray*"); break;
551 case 'F': fprintf (file, "java_floatarray*"); break;
552 case 'D': fprintf (file, "java_doublearray*"); break;
554 case '[': fprintf(file, "java_objectarray*");
555 while ((c = utf_nextu2(&utf_ptr)) == '[');
557 while (utf_nextu2(&utf_ptr) != ';');
560 case 'L': fprintf(file, "java_objectarray*");
561 while (utf_nextu2(&utf_ptr) != ';');
564 log_text("invalid type descriptor");
570 addoutputsize ( sizeof(java_objectheader*));
571 fprintf (file, "struct ");
572 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
577 log_text("Unknown type in field descriptor");
585 /***** determine the number of entries of a utf string in the ident chain *****/
587 static int searchidentchain_utf(utf *ident)
589 utf *u = chain_first(ident_chain); /* first element of list */
593 if (u==ident) count++; /* string found */
594 u = chain_next(ident_chain); /* next element in list */
601 /************** print structure for direct access to objects ******************/
603 static void printfields(classinfo *c)
610 addoutputsize(sizeof(java_objectheader));
611 fprintf(file, " java_objectheader header;\n");
615 printfields(c->super.cls);
617 for (i = 0; i < c->fieldscount; i++) {
620 if (!(f->flags & ACC_STATIC)) {
622 printtype(f->descriptor->text);
624 utf_fprint(file, f->name);
626 /* rename multiple fieldnames */
627 if ((ident_count = searchidentchain_utf(f->name)))
628 fprintf(file, "%d", ident_count - 1);
629 chain_addlast(ident_chain, f->name);
631 fprintf(file, ";\n");
637 /***************** store prototype for native method in file ******************/
639 void printmethod(methodinfo *m)
644 /* search for return-type in descriptor */
645 utf_ptr = m->descriptor->text;
646 while (utf_nextu2(&utf_ptr) != ')');
649 fprintf(file, "\n/*\n * Class: ");
650 utf_fprint(file, m->class->name);
651 fprintf(file, "\n * Method: ");
652 utf_fprint(file, m->name);
653 fprintf(file, "\n * Signature: ");
654 utf_fprint(file, m->descriptor);
655 fprintf(file, "\n */\n");
657 /* create prototype */
658 fprintf(file, "JNIEXPORT ");
660 fprintf(file, " JNICALL Java_");
661 printID(m->class->name);
663 chain_addlast(ident_chain, m->name);
667 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
668 fprintf(file, "(JNIEnv *env");
670 utf_ptr = m->descriptor->text + 1;
672 if (!(m->flags & ACC_STATIC)) {
673 fprintf(file, ", struct ");
674 printID(m->class->name);
675 fprintf(file, "* this");
678 fprintf(file, ", jclass clazz");
681 if ((*utf_ptr) != ')') fprintf(file, ", ");
683 while ((*utf_ptr) != ')') {
684 utf_ptr = printtype(utf_ptr);
685 fprintf(file, " par%d", paramnum++);
686 if ((*utf_ptr)!=')') fprintf(file, ", ");
689 fprintf(file, ");\n\n");
693 /******* remove package-name in fully-qualified classname *********************/
695 void gen_header_filename(char *buffer, utf *u)
699 for (i = 0; i < utf_strlen(u); i++) {
700 if ((u->text[i] == '/') || (u->text[i] == '$')) {
701 buffer[i] = '_'; /* convert '$' and '/' to '_' */
704 buffer[i] = u->text[i];
707 buffer[utf_strlen(u)] = '\0';
711 /* create headerfile for classes and store native methods in chain ************/
713 void headerfile_generate(classinfo *c, char *opt_directory)
715 char header_filename[1024] = "";
716 char classname[1024];
717 char uclassname[1024];
722 u2 nativelyoverloaded;
724 /* store class in chain */
725 chain_addlast(nativeclass_chain, c);
727 /* open headerfile for class */
728 gen_header_filename(classname, c->name);
730 /* create chain for renaming fields */
731 ident_chain = chain_new();
734 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
737 sprintf(header_filename, "%s.h", classname);
740 file = fopen(header_filename, "w");
742 log_text("Can not open file to store header information");
746 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
748 /* convert to uppercase */
749 for (i = 0; classname[i]; i++) {
750 uclassname[i] = toupper(classname[i]);
752 uclassname[i] = '\0';
754 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
756 /* create structure for direct access to objects */
757 fprintf(file, "/* Structure information for class: ");
758 utf_fprint(file, c->name);
759 fprintf(file, " */\n\n");
760 fprintf(file, "typedef struct ");
762 fprintf(file, " {\n");
770 fprintf(file, ";\n\n");
772 /* create chain for renaming overloaded methods */
773 chain_free(ident_chain);
774 ident_chain = chain_new();
776 /* create method-prototypes */
778 /* find overloaded methods */
779 for (i = 0; i < c->methodscount; i++) {
781 m = &(c->methods[i]);
783 if (!(m->flags & ACC_NATIVE)) continue;
784 if (!m->nativelyoverloaded) {
785 nativelyoverloaded=false;
786 for (i2=i+1;i2<c->methodscount; i2++) {
787 m2 = &(c->methods[i2]);
788 if (!(m2->flags & ACC_NATIVE)) continue;
789 if (m->name==m2->name) {
790 m2->nativelyoverloaded=true;
791 nativelyoverloaded=true;
794 m->nativelyoverloaded=nativelyoverloaded;
799 for (i = 0; i < c->methodscount; i++) {
801 m = &(c->methods[i]);
803 if (m->flags & ACC_NATIVE) {
804 chain_addlast(nativemethod_chain, m);
809 chain_free(ident_chain);
811 fprintf(file, "#endif\n\n");
817 /******** print classname, '$' used to seperate inner-class name ***********/
819 void print_classname(classinfo *clazz)
821 utf *u = clazz->name;
822 char *endpos = u->text + u->blength;
823 char *utf_ptr = u->text;
826 while (utf_ptr < endpos) {
827 if ((c = utf_nextu2(&utf_ptr)) == '_')
836 * These are local overrides for various environment variables in Emacs.
837 * Please do not remove this and leave it at the end of the file, where
838 * Emacs will automagically detect them.
839 * ---------------------------------------------------------------------
842 * indent-tabs-mode: t