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 3188 2005-09-16 11:33:21Z 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 *_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)
179 /* exception functions ********************************************************/
181 /* these should not be called */
183 void throw_exception(void) { assert(0); }
184 void throw_exception_exit(void) { assert(0); }
186 java_objectheader *new_verifyerror(methodinfo *m, const char *message)
190 /* keep compiler happy */
195 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
199 /* keep compiler happy */
205 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
209 fprintf(stderr, "%s: ", exception);
211 va_start(ap, message);
212 vfprintf(stderr, message, ap);
221 java_objectheader *new_exception(const char *classname)
223 fprintf(stderr, "%s\n", classname);
226 /* keep compiler happy */
232 java_objectheader *new_exception_message(const char *classname, const char *message)
234 fprintf(stderr, "%s: %s\n", classname, message);
237 /* keep compiler happy */
243 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
245 fprintf(stderr, "%s: ", classname);
246 utf_display(message);
251 /* keep compiler happy */
257 java_objectheader *new_exception_javastring(const char *classname,
258 java_lang_String *message)
260 fprintf(stderr, "%s: ", classname);
261 /* TODO print message */
266 /* keep compiler happy */
272 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
276 utf_display(c->name);
277 fprintf(stderr, ": ");
279 va_start(ap, message);
280 vfprintf(stderr, message, ap);
287 /* keep compiler happy */
293 java_objectheader *new_internalerror(const char *message, ...)
297 fprintf(stderr, "%s: ", string_java_lang_InternalError);
299 va_start(ap, message);
300 vfprintf(stderr, message, ap);
305 /* keep compiler happy */
311 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
315 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
317 utf_display(c->name);
318 fprintf(stderr, ": ");
320 va_start(ap, message);
321 vfprintf(stderr, message, ap);
326 /* keep compiler happy */
332 java_objectheader *new_negativearraysizeexception(void)
334 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
337 /* keep compiler happy */
343 java_objectheader *new_nullpointerexception(void)
345 fprintf(stderr, "%s", string_java_lang_NullPointerException);
348 /* keep compiler happy */
354 /* machine dependent stuff ****************************************************/
356 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
357 threadcritnode asm_criticalsections;
358 void thread_restartcriticalsection(ucontext_t *uc) {}
361 void md_param_alloc(methoddesc *md) {}
364 /************************ global variables **********************/
366 chain *nativemethod_chain; /* chain with native methods */
367 chain *nativeclass_chain; /* chain with processed classes */
368 chain *ident_chain; /* chain with method and field names in current class */
370 static u4 outputsize;
371 static bool dopadding;
374 static void printIDpart(int c)
376 if ((c >= 'a' && c <= 'z') ||
377 (c >= 'A' && c <= 'Z') ||
378 (c >= '0' && c <= '9') ||
388 char *utf_ptr = u->text;
391 for (i = 0; i < utf_strlen(u); i++)
392 printIDpart(utf_nextu2(&utf_ptr));
396 static void addoutputsize (int len)
399 if (!dopadding) return;
401 newsize = ALIGN(outputsize, len);
403 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
404 outputsize = newsize;
408 void printOverloadPart(utf *desc)
410 char *utf_ptr=desc->text;
415 while ((c = utf_nextu2(&utf_ptr)) != ')') {
425 fprintf(file, "%c", (char) c);
432 while ((c = utf_nextu2(&utf_ptr)) != ';')
439 log_text("invalid method descriptor");
445 static char *printtype(char *utf_ptr)
449 switch (utf_nextu2(&utf_ptr)) {
450 case 'V': fprintf (file, "void");
456 case 'Z': addoutputsize (4);
457 fprintf (file, "s4");
459 case 'J': addoutputsize (8);
460 fprintf (file, "s8");
462 case 'F': addoutputsize (4);
463 fprintf (file, "float");
465 case 'D': addoutputsize (8);
466 fprintf (file, "double");
469 addoutputsize ( sizeof(java_arrayheader*) );
470 switch (utf_nextu2(&utf_ptr)) {
471 case 'I': fprintf (file, "java_intarray*"); break;
472 case 'J': fprintf (file, "java_longarray*"); break;
473 case 'Z': fprintf (file, "java_booleanarray*"); break;
474 case 'B': fprintf (file, "java_bytearray*"); break;
475 case 'S': fprintf (file, "java_shortarray*"); break;
476 case 'C': fprintf (file, "java_chararray*"); break;
477 case 'F': fprintf (file, "java_floatarray*"); break;
478 case 'D': fprintf (file, "java_doublearray*"); break;
480 case '[': fprintf(file, "java_objectarray*");
481 while ((c = utf_nextu2(&utf_ptr)) == '[');
483 while (utf_nextu2(&utf_ptr) != ';');
486 case 'L': fprintf(file, "java_objectarray*");
487 while (utf_nextu2(&utf_ptr) != ';');
490 log_text("invalid type descriptor");
496 addoutputsize ( sizeof(java_objectheader*));
497 fprintf (file, "struct ");
498 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
503 log_text("Unknown type in field descriptor");
511 /***** determine the number of entries of a utf string in the ident chain *****/
513 static int searchidentchain_utf(utf *ident)
515 utf *u = chain_first(ident_chain); /* first element of list */
519 if (u==ident) count++; /* string found */
520 u = chain_next(ident_chain); /* next element in list */
527 /************** print structure for direct access to objects ******************/
529 static void printfields(classinfo *c)
536 addoutputsize(sizeof(java_objectheader));
537 fprintf(file, " java_objectheader header;\n");
541 printfields(c->super.cls);
543 for (i = 0; i < c->fieldscount; i++) {
546 if (!(f->flags & ACC_STATIC)) {
548 printtype(f->descriptor->text);
550 utf_fprint(file, f->name);
552 /* rename multiple fieldnames */
553 if ((ident_count = searchidentchain_utf(f->name)))
554 fprintf(file, "%d", ident_count - 1);
555 chain_addlast(ident_chain, f->name);
557 fprintf(file, ";\n");
563 /***************** store prototype for native method in file ******************/
565 void printmethod(methodinfo *m)
570 /* search for return-type in descriptor */
571 utf_ptr = m->descriptor->text;
572 while (utf_nextu2(&utf_ptr) != ')');
575 fprintf(file, "\n/*\n * Class: ");
576 utf_fprint(file, m->class->name);
577 fprintf(file, "\n * Method: ");
578 utf_fprint(file, m->name);
579 fprintf(file, "\n * Signature: ");
580 utf_fprint(file, m->descriptor);
581 fprintf(file, "\n */\n");
583 /* create prototype */
584 fprintf(file, "JNIEXPORT ");
586 fprintf(file, " JNICALL Java_");
587 printID(m->class->name);
589 chain_addlast(ident_chain, m->name);
593 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
594 fprintf(file, "(JNIEnv *env");
596 utf_ptr = m->descriptor->text + 1;
598 if (!(m->flags & ACC_STATIC)) {
599 fprintf(file, ", struct ");
600 printID(m->class->name);
601 fprintf(file, "* this");
604 fprintf(file, ", jclass clazz");
607 if ((*utf_ptr) != ')') fprintf(file, ", ");
609 while ((*utf_ptr) != ')') {
610 utf_ptr = printtype(utf_ptr);
611 fprintf(file, " par%d", paramnum++);
612 if ((*utf_ptr)!=')') fprintf(file, ", ");
615 fprintf(file, ");\n\n");
619 /******* remove package-name in fully-qualified classname *********************/
621 void gen_header_filename(char *buffer, utf *u)
625 for (i = 0; i < utf_strlen(u); i++) {
626 if ((u->text[i] == '/') || (u->text[i] == '$')) {
627 buffer[i] = '_'; /* convert '$' and '/' to '_' */
630 buffer[i] = u->text[i];
633 buffer[utf_strlen(u)] = '\0';
637 /* create headerfile for classes and store native methods in chain ************/
639 void headerfile_generate(classinfo *c, char *opt_directory)
641 char header_filename[1024] = "";
642 char classname[1024];
643 char uclassname[1024];
648 u2 nativelyoverloaded;
650 /* store class in chain */
651 chain_addlast(nativeclass_chain, c);
653 /* open headerfile for class */
654 gen_header_filename(classname, c->name);
656 /* create chain for renaming fields */
657 ident_chain = chain_new();
660 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
663 sprintf(header_filename, "%s.h", classname);
666 file = fopen(header_filename, "w");
668 log_text("Can not open file to store header information");
672 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
674 /* convert to uppercase */
675 for (i = 0; classname[i]; i++) {
676 uclassname[i] = toupper(classname[i]);
678 uclassname[i] = '\0';
680 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
682 /* create structure for direct access to objects */
683 fprintf(file, "/* Structure information for class: ");
684 utf_fprint(file, c->name);
685 fprintf(file, " */\n\n");
686 fprintf(file, "typedef struct ");
688 fprintf(file, " {\n");
696 fprintf(file, ";\n\n");
698 /* create chain for renaming overloaded methods */
699 chain_free(ident_chain);
700 ident_chain = chain_new();
702 /* create method-prototypes */
704 /* find overloaded methods */
705 for (i = 0; i < c->methodscount; i++) {
707 m = &(c->methods[i]);
709 if (!(m->flags & ACC_NATIVE)) continue;
710 if (!m->nativelyoverloaded) {
711 nativelyoverloaded=false;
712 for (i2=i+1;i2<c->methodscount; i2++) {
713 m2 = &(c->methods[i2]);
714 if (!(m2->flags & ACC_NATIVE)) continue;
715 if (m->name==m2->name) {
716 m2->nativelyoverloaded=true;
717 nativelyoverloaded=true;
720 m->nativelyoverloaded=nativelyoverloaded;
725 for (i = 0; i < c->methodscount; i++) {
727 m = &(c->methods[i]);
729 if (m->flags & ACC_NATIVE) {
730 chain_addlast(nativemethod_chain, m);
735 chain_free(ident_chain);
737 fprintf(file, "#endif\n\n");
743 /******** print classname, '$' used to seperate inner-class name ***********/
745 void print_classname(classinfo *clazz)
747 utf *u = clazz->name;
748 char *endpos = u->text + u->blength;
749 char *utf_ptr = u->text;
752 while (utf_ptr < endpos) {
753 if ((c = utf_nextu2(&utf_ptr)) == '_')
762 * These are local overrides for various environment variables in Emacs.
763 * Please do not remove this and leave it at the end of the file, where
764 * Emacs will automagically detect them.
765 * ---------------------------------------------------------------------
768 * indent-tabs-mode: t