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 3742 2005-11-22 23:40:30Z 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_arraycheckcast(u1 *sp) {}
108 s4 asm_builtin_f2i(float a) { return 0; }
109 s8 asm_builtin_f2l(float a) { return 0; }
110 s4 asm_builtin_d2i(double a) { return 0; }
111 s8 asm_builtin_d2l(double a) { return 0; }
113 void use_class_as_object(void) {}
115 #if defined(__MIPS__)
116 long compare_and_swap(long *p, long oldval, long newval)
127 u1 *createcompilerstub(methodinfo *m) { return NULL; }
128 u1 *codegen_createnativestub(functionptr f, methodinfo *m) { return NULL; }
130 void removecompilerstub(u1 *stub) {}
131 void removenativestub(u1 *stub) {}
133 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
134 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
136 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
138 void asm_handle_builtin_exception(classinfo *c) {}
139 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
142 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
147 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
149 return typecheck_TRUE;
152 void typeinfo_init_classinfo(typeinfo *info,classinfo *c)
156 bool typeinfo_init_class(typeinfo *info,classref_or_classinfo c)
161 void typeinfo_print(FILE *file,typeinfo *info,int indent) {}
163 void exceptions_print_exception(java_objectheader *xptr) {}
164 void stacktrace_print_trace(java_objectheader *xptr) {}
167 /* exception functions ********************************************************/
169 /* these should not be called */
171 void throw_main_exception_exit(void) { assert(0); }
172 void throw_exception(void) { assert(0); }
173 void throw_exception_exit(void) { assert(0); }
175 java_objectheader *new_verifyerror(methodinfo *m, const char *message)
179 /* keep compiler happy */
184 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
188 /* keep compiler happy */
194 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
198 fprintf(stderr, "%s: ", exception);
200 va_start(ap, message);
201 vfprintf(stderr, message, ap);
210 void exceptions_throw_outofmemory_exit(void)
212 fprintf(stderr, "java.lang.InternalError: Out of memory\n");
217 java_objectheader *new_exception(const char *classname)
219 fprintf(stderr, "%s\n", classname);
222 /* keep compiler happy */
228 java_objectheader *new_exception_message(const char *classname, const char *message)
230 fprintf(stderr, "%s: %s\n", classname, message);
233 /* keep compiler happy */
239 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
241 fprintf(stderr, "%s: ", classname);
242 utf_display(message);
247 /* keep compiler happy */
253 java_objectheader *new_exception_javastring(const char *classname,
254 java_lang_String *message)
256 fprintf(stderr, "%s: ", classname);
257 /* TODO print message */
262 /* keep compiler happy */
268 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
272 utf_display(c->name);
273 fprintf(stderr, ": ");
275 va_start(ap, message);
276 vfprintf(stderr, message, ap);
283 /* keep compiler happy */
289 java_objectheader *new_classnotfoundexception(utf *name)
291 fprintf(stderr, "java.lang.ClassNotFoundException: ");
292 utf_fprint(stderr, name);
297 /* keep compiler happy */
303 java_objectheader *new_noclassdeffounderror(utf *name)
305 fprintf(stderr, "java.lang.NoClassDefFoundError: ");
306 utf_fprint(stderr, name);
311 /* keep compiler happy */
317 java_objectheader *exceptions_new_nosuchmethoderror(classinfo *c,
318 utf *name, utf *desc)
320 fprintf(stderr, "java.lang.NoSuchMethodError: ");
321 utf_fprint(stderr, c->name);
322 fprintf(stderr, ".");
323 utf_fprint(stderr, name);
324 utf_fprint(stderr, desc);
329 /* keep compiler happy */
335 java_objectheader *new_internalerror(const char *message, ...)
339 fprintf(stderr, "%s: ", string_java_lang_InternalError);
341 va_start(ap, message);
342 vfprintf(stderr, message, ap);
347 /* keep compiler happy */
353 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
357 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
359 utf_display(c->name);
360 fprintf(stderr, ": ");
362 va_start(ap, message);
363 vfprintf(stderr, message, ap);
368 /* keep compiler happy */
374 java_objectheader *new_illegalmonitorstateexception(void)
376 fprintf(stderr, "%s", string_java_lang_IllegalMonitorStateException);
379 /* keep compiler happy */
385 java_objectheader *new_negativearraysizeexception(void)
387 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
390 /* keep compiler happy */
396 java_objectheader *new_nullpointerexception(void)
398 fprintf(stderr, "%s", string_java_lang_NullPointerException);
401 /* keep compiler happy */
406 void classnotfoundexception_to_noclassdeffounderror(void)
410 /* machine dependent stuff ****************************************************/
412 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
413 threadcritnode asm_criticalsections;
414 void thread_restartcriticalsection(ucontext_t *uc) {}
417 void md_param_alloc(methoddesc *md) {}
420 /************************ global variables **********************/
422 chain *nativemethod_chain; /* chain with native methods */
423 chain *nativeclass_chain; /* chain with processed classes */
424 chain *ident_chain; /* chain with method and field names in current class */
426 static u4 outputsize;
427 static bool dopadding;
430 static void printIDpart(int c)
432 if ((c >= 'a' && c <= 'z') ||
433 (c >= 'A' && c <= 'Z') ||
434 (c >= '0' && c <= '9') ||
444 char *utf_ptr = u->text;
447 for (i = 0; i < utf_strlen(u); i++)
448 printIDpart(utf_nextu2(&utf_ptr));
452 static void addoutputsize (int len)
455 if (!dopadding) return;
457 newsize = ALIGN(outputsize, len);
459 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
460 outputsize = newsize;
464 void printOverloadPart(utf *desc)
466 char *utf_ptr=desc->text;
471 while ((c = utf_nextu2(&utf_ptr)) != ')') {
481 fprintf(file, "%c", (char) c);
488 while ((c = utf_nextu2(&utf_ptr)) != ';')
495 log_text("invalid method descriptor");
501 static char *printtype(char *utf_ptr)
505 switch (utf_nextu2(&utf_ptr)) {
506 case 'V': fprintf (file, "void");
512 case 'Z': addoutputsize (4);
513 fprintf (file, "s4");
515 case 'J': addoutputsize (8);
516 fprintf (file, "s8");
518 case 'F': addoutputsize (4);
519 fprintf (file, "float");
521 case 'D': addoutputsize (8);
522 fprintf (file, "double");
525 addoutputsize ( sizeof(java_arrayheader*) );
526 switch (utf_nextu2(&utf_ptr)) {
527 case 'I': fprintf (file, "java_intarray*"); break;
528 case 'J': fprintf (file, "java_longarray*"); break;
529 case 'Z': fprintf (file, "java_booleanarray*"); break;
530 case 'B': fprintf (file, "java_bytearray*"); break;
531 case 'S': fprintf (file, "java_shortarray*"); break;
532 case 'C': fprintf (file, "java_chararray*"); break;
533 case 'F': fprintf (file, "java_floatarray*"); break;
534 case 'D': fprintf (file, "java_doublearray*"); break;
536 case '[': fprintf(file, "java_objectarray*");
537 while ((c = utf_nextu2(&utf_ptr)) == '[');
539 while (utf_nextu2(&utf_ptr) != ';');
542 case 'L': fprintf(file, "java_objectarray*");
543 while (utf_nextu2(&utf_ptr) != ';');
546 log_text("invalid type descriptor");
552 addoutputsize ( sizeof(java_objectheader*));
553 fprintf (file, "struct ");
554 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
559 log_text("Unknown type in field descriptor");
567 /***** determine the number of entries of a utf string in the ident chain *****/
569 static int searchidentchain_utf(utf *ident)
571 utf *u = chain_first(ident_chain); /* first element of list */
575 if (u==ident) count++; /* string found */
576 u = chain_next(ident_chain); /* next element in list */
583 /************** print structure for direct access to objects ******************/
585 static void printfields(classinfo *c)
592 addoutputsize(sizeof(java_objectheader));
593 fprintf(file, " java_objectheader header;\n");
597 printfields(c->super.cls);
599 for (i = 0; i < c->fieldscount; i++) {
602 if (!(f->flags & ACC_STATIC)) {
604 printtype(f->descriptor->text);
606 utf_fprint(file, f->name);
608 /* rename multiple fieldnames */
609 if ((ident_count = searchidentchain_utf(f->name)))
610 fprintf(file, "%d", ident_count - 1);
611 chain_addlast(ident_chain, f->name);
613 fprintf(file, ";\n");
619 /***************** store prototype for native method in file ******************/
621 void printmethod(methodinfo *m)
626 /* search for return-type in descriptor */
627 utf_ptr = m->descriptor->text;
628 while (utf_nextu2(&utf_ptr) != ')');
631 fprintf(file, "\n/*\n * Class: ");
632 utf_fprint(file, m->class->name);
633 fprintf(file, "\n * Method: ");
634 utf_fprint(file, m->name);
635 fprintf(file, "\n * Signature: ");
636 utf_fprint(file, m->descriptor);
637 fprintf(file, "\n */\n");
639 /* create prototype */
640 fprintf(file, "JNIEXPORT ");
642 fprintf(file, " JNICALL Java_");
643 printID(m->class->name);
645 chain_addlast(ident_chain, m->name);
649 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
650 fprintf(file, "(JNIEnv *env");
652 utf_ptr = m->descriptor->text + 1;
654 if (!(m->flags & ACC_STATIC)) {
655 fprintf(file, ", struct ");
656 printID(m->class->name);
657 fprintf(file, "* this");
660 fprintf(file, ", jclass clazz");
663 if ((*utf_ptr) != ')') fprintf(file, ", ");
665 while ((*utf_ptr) != ')') {
666 utf_ptr = printtype(utf_ptr);
667 fprintf(file, " par%d", paramnum++);
668 if ((*utf_ptr)!=')') fprintf(file, ", ");
671 fprintf(file, ");\n\n");
675 /******* remove package-name in fully-qualified classname *********************/
677 void gen_header_filename(char *buffer, utf *u)
681 for (i = 0; i < utf_strlen(u); i++) {
682 if ((u->text[i] == '/') || (u->text[i] == '$')) {
683 buffer[i] = '_'; /* convert '$' and '/' to '_' */
686 buffer[i] = u->text[i];
689 buffer[utf_strlen(u)] = '\0';
693 /* create headerfile for classes and store native methods in chain ************/
695 void headerfile_generate(classinfo *c, char *opt_directory)
697 char header_filename[1024] = "";
698 char classname[1024];
699 char uclassname[1024];
704 u2 nativelyoverloaded;
706 /* store class in chain */
707 chain_addlast(nativeclass_chain, c);
709 /* open headerfile for class */
710 gen_header_filename(classname, c->name);
712 /* create chain for renaming fields */
713 ident_chain = chain_new();
716 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
719 sprintf(header_filename, "%s.h", classname);
722 file = fopen(header_filename, "w");
724 log_text("Can not open file to store header information");
728 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
730 /* convert to uppercase */
731 for (i = 0; classname[i]; i++) {
732 uclassname[i] = toupper(classname[i]);
734 uclassname[i] = '\0';
736 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
738 /* create structure for direct access to objects */
739 fprintf(file, "/* Structure information for class: ");
740 utf_fprint(file, c->name);
741 fprintf(file, " */\n\n");
742 fprintf(file, "typedef struct ");
744 fprintf(file, " {\n");
752 fprintf(file, ";\n\n");
754 /* create chain for renaming overloaded methods */
755 chain_free(ident_chain);
756 ident_chain = chain_new();
758 /* create method-prototypes */
760 /* find overloaded methods */
761 for (i = 0; i < c->methodscount; i++) {
763 m = &(c->methods[i]);
765 if (!(m->flags & ACC_NATIVE)) continue;
766 if (!m->nativelyoverloaded) {
767 nativelyoverloaded=false;
768 for (i2=i+1;i2<c->methodscount; i2++) {
769 m2 = &(c->methods[i2]);
770 if (!(m2->flags & ACC_NATIVE)) continue;
771 if (m->name==m2->name) {
772 m2->nativelyoverloaded=true;
773 nativelyoverloaded=true;
776 m->nativelyoverloaded=nativelyoverloaded;
781 for (i = 0; i < c->methodscount; i++) {
783 m = &(c->methods[i]);
785 if (m->flags & ACC_NATIVE) {
786 chain_addlast(nativemethod_chain, m);
791 chain_free(ident_chain);
793 fprintf(file, "#endif\n\n");
799 /******** print classname, '$' used to seperate inner-class name ***********/
801 void print_classname(classinfo *clazz)
803 utf *u = clazz->name;
804 char *endpos = u->text + u->blength;
805 char *utf_ptr = u->text;
808 while (utf_ptr < endpos) {
809 if ((c = utf_nextu2(&utf_ptr)) == '_')
818 * These are local overrides for various environment variables in Emacs.
819 * Please do not remove this and leave it at the end of the file, where
820 * Emacs will automagically detect them.
821 * ---------------------------------------------------------------------
824 * indent-tabs-mode: t