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 2858 2005-06-28 18:36:08Z 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 /* for raising exceptions from native methods */
72 #if !defined(USE_THREADS) || !defined(NATIVE_THREADS)
73 java_objectheader *_exceptionptr = NULL;
77 /******* replace some external functions *********/
79 functionptr native_findfunction(utf *cname, utf *mname, utf *desc, bool isstatic)
82 java_objectheader *native_new_and_init(classinfo *c) { return NULL; }
83 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s) { return NULL; }
84 java_objectheader *native_new_and_init_int(classinfo *c, s4 i) { return NULL; }
85 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t) { return NULL; }
88 java_objectheader *asm_calljavafunction(methodinfo *m, void *arg1, void *arg2,
89 void *arg3, void *arg4) { return NULL; }
91 void asm_call_jit_compiler(void) {}
93 /* code patching functions */
94 void patcher_builtin_new(u1 *sp) {}
95 void patcher_builtin_newarray(u1 *sp) {}
96 void patcher_builtin_arraycheckcast(u1 *sp) {}
97 void patcher_builtin_arrayinstanceof(u1 *sp) {}
99 s4 asm_builtin_idiv(s4 a, s4 b) { return 0; }
100 s4 asm_builtin_irem(s4 a, s4 b) { return 0; }
101 s8 asm_builtin_ldiv(s8 a, s8 b) { return 0; }
102 s8 asm_builtin_lrem(s8 a, s8 b) { return 0; }
104 s4 asm_builtin_f2i(float a) { return 0; }
105 s8 asm_builtin_f2l(float a) { return 0; }
106 s4 asm_builtin_d2i(double a) { return 0; }
107 s8 asm_builtin_d2l(double a) { return 0; }
109 void use_class_as_object(void) {}
110 void asm_builtin_monitorenter(java_objectheader *o) {}
111 void *asm_builtin_monitorexit(java_objectheader *o) { return NULL; }
113 s4 asm_builtin_arraycheckcast(java_objectheader *obj, vftbl_t *target)
118 #if defined(__MIPS__)
119 long compare_and_swap(long *p, long oldval, long newval)
130 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
132 functionptr createcompilerstub(methodinfo *m) { return NULL; }
133 functionptr codegen_createnativestub(functionptr f, methodinfo *m) { return NULL; }
135 void removecompilerstub(functionptr stub) {}
136 void removenativestub(functionptr stub) {}
138 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
139 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
141 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
143 void asm_handle_builtin_exception(classinfo *c) {}
144 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
147 void setVMClassField(classinfo *c) {}
148 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
153 typecheck_result typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
155 return typecheck_TRUE;
158 bool typeinfo_init_class(typeinfo *info,classref_or_classinfo c)
163 void typeinfo_print(FILE *file,typeinfo *info,int indent)
167 /* exception functions ********************************************************/
169 /* these should not be called */
171 void throw_exception(void) { assert(0); }
172 void throw_exception_exit(void) { assert(0); }
174 java_objectheader *new_verifyerror(methodinfo *m, const char *message)
178 /* keep compiler happy */
183 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
187 /* keep compiler happy */
193 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
197 fprintf(stderr, "%s: ", exception);
199 va_start(ap, message);
200 vfprintf(stderr, message, ap);
207 java_objectheader *new_exception(const char *classname)
209 fprintf(stderr, "%s\n", classname);
212 /* keep compiler happy */
218 java_objectheader *new_exception_message(const char *classname, const char *message)
220 fprintf(stderr, "%s: %s\n", classname, message);
223 /* keep compiler happy */
229 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
231 fprintf(stderr, "%s: ", classname);
232 utf_display(message);
237 /* keep compiler happy */
243 java_objectheader *new_exception_javastring(const char *classname,
244 java_lang_String *message)
246 fprintf(stderr, "%s: ", classname);
247 /* TODO print message */
252 /* keep compiler happy */
258 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
262 utf_display(c->name);
263 fprintf(stderr, ": ");
265 va_start(ap, message);
266 vfprintf(stderr, message, ap);
273 /* keep compiler happy */
279 java_objectheader *new_internalerror(const char *message, ...)
283 fprintf(stderr, "%s: ", string_java_lang_InternalError);
285 va_start(ap, message);
286 vfprintf(stderr, message, ap);
291 /* keep compiler happy */
297 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
301 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
303 utf_display(c->name);
304 fprintf(stderr, ": ");
306 va_start(ap, message);
307 vfprintf(stderr, message, ap);
312 /* keep compiler happy */
318 java_objectheader *new_negativearraysizeexception(void)
320 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
323 /* keep compiler happy */
329 java_objectheader *new_nullpointerexception(void)
331 fprintf(stderr, "%s", string_java_lang_NullPointerException);
334 /* keep compiler happy */
340 /* machine dependent stuff ****************************************************/
342 #if defined(__DARWIN__)
343 int cacao_catch_Handler(void) {}
346 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
347 threadcritnode asm_criticalsections;
348 void thread_restartcriticalsection(ucontext_t *uc) {}
351 void md_param_alloc(methoddesc *md) {}
354 /************************ global variables **********************/
356 chain *nativemethod_chain; /* chain with native methods */
357 chain *nativeclass_chain; /* chain with processed classes */
358 chain *ident_chain; /* chain with method and field names in current class */
360 static u4 outputsize;
361 static bool dopadding;
364 static void printIDpart(int c)
366 if ((c >= 'a' && c <= 'z') ||
367 (c >= 'A' && c <= 'Z') ||
368 (c >= '0' && c <= '9') ||
378 char *utf_ptr = u->text;
381 for (i = 0; i < utf_strlen(u); i++)
382 printIDpart(utf_nextu2(&utf_ptr));
386 static void addoutputsize (int len)
389 if (!dopadding) return;
391 newsize = ALIGN(outputsize, len);
393 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
394 outputsize = newsize;
398 void printOverloadPart(utf *desc)
400 char *utf_ptr=desc->text;
405 while ((c = utf_nextu2(&utf_ptr)) != ')') {
415 fprintf(file, "%c", (char) c);
422 while ((c = utf_nextu2(&utf_ptr)) != ';')
429 log_text("invalid method descriptor");
435 static char *printtype(char *utf_ptr)
439 switch (utf_nextu2(&utf_ptr)) {
440 case 'V': fprintf (file, "void");
446 case 'Z': addoutputsize (4);
447 fprintf (file, "s4");
449 case 'J': addoutputsize (8);
450 fprintf (file, "s8");
452 case 'F': addoutputsize (4);
453 fprintf (file, "float");
455 case 'D': addoutputsize (8);
456 fprintf (file, "double");
459 addoutputsize ( sizeof(java_arrayheader*) );
460 switch (utf_nextu2(&utf_ptr)) {
461 case 'I': fprintf (file, "java_intarray*"); break;
462 case 'J': fprintf (file, "java_longarray*"); break;
463 case 'Z': fprintf (file, "java_booleanarray*"); break;
464 case 'B': fprintf (file, "java_bytearray*"); break;
465 case 'S': fprintf (file, "java_shortarray*"); break;
466 case 'C': fprintf (file, "java_chararray*"); break;
467 case 'F': fprintf (file, "java_floatarray*"); break;
468 case 'D': fprintf (file, "java_doublearray*"); break;
470 case '[': fprintf(file, "java_objectarray*");
471 while ((c = utf_nextu2(&utf_ptr)) == '[');
473 while (utf_nextu2(&utf_ptr) != ';');
476 case 'L': fprintf(file, "java_objectarray*");
477 while (utf_nextu2(&utf_ptr) != ';');
480 log_text("invalid type descriptor");
486 addoutputsize ( sizeof(java_objectheader*));
487 fprintf (file, "struct ");
488 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
493 log_text("Unknown type in field descriptor");
501 /***** determine the number of entries of a utf string in the ident chain *****/
503 static int searchidentchain_utf(utf *ident)
505 utf *u = chain_first(ident_chain); /* first element of list */
509 if (u==ident) count++; /* string found */
510 u = chain_next(ident_chain); /* next element in list */
517 /************** print structure for direct access to objects ******************/
519 static void printfields(classinfo *c)
526 addoutputsize(sizeof(java_objectheader));
527 fprintf(file, " java_objectheader header;\n");
531 printfields(c->super.cls);
533 for (i = 0; i < c->fieldscount; i++) {
536 if (!(f->flags & ACC_STATIC)) {
538 printtype(f->descriptor->text);
540 utf_fprint(file, f->name);
542 /* rename multiple fieldnames */
543 if ((ident_count = searchidentchain_utf(f->name)))
544 fprintf(file, "%d", ident_count - 1);
545 chain_addlast(ident_chain, f->name);
547 fprintf(file, ";\n");
553 /***************** store prototype for native method in file ******************/
555 void printmethod(methodinfo *m)
560 /* search for return-type in descriptor */
561 utf_ptr = m->descriptor->text;
562 while (utf_nextu2(&utf_ptr) != ')');
565 fprintf(file, "\n/*\n * Class: ");
566 utf_fprint(file, m->class->name);
567 fprintf(file, "\n * Method: ");
568 utf_fprint(file, m->name);
569 fprintf(file, "\n * Signature: ");
570 utf_fprint(file, m->descriptor);
571 fprintf(file, "\n */\n");
573 /* create prototype */
574 fprintf(file, "JNIEXPORT ");
576 fprintf(file, " JNICALL Java_");
577 printID(m->class->name);
579 chain_addlast(ident_chain, m->name);
583 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
584 fprintf(file, "(JNIEnv *env");
586 utf_ptr = m->descriptor->text + 1;
588 if (!(m->flags & ACC_STATIC)) {
589 fprintf(file, ", struct ");
590 printID(m->class->name);
591 fprintf(file, "* this");
594 fprintf(file, ", jclass clazz");
597 if ((*utf_ptr) != ')') fprintf(file, ", ");
599 while ((*utf_ptr) != ')') {
600 utf_ptr = printtype(utf_ptr);
601 fprintf(file, " par%d", paramnum++);
602 if ((*utf_ptr)!=')') fprintf(file, ", ");
605 fprintf(file, ");\n\n");
609 /******* remove package-name in fully-qualified classname *********************/
611 void gen_header_filename(char *buffer, utf *u)
615 for (i = 0; i < utf_strlen(u); i++) {
616 if ((u->text[i] == '/') || (u->text[i] == '$')) {
617 buffer[i] = '_'; /* convert '$' and '/' to '_' */
620 buffer[i] = u->text[i];
623 buffer[utf_strlen(u)] = '\0';
627 /* create headerfile for classes and store native methods in chain ************/
629 void headerfile_generate(classinfo *c, char *opt_directory)
631 char header_filename[1024] = "";
632 char classname[1024];
633 char uclassname[1024];
638 u2 nativelyoverloaded;
640 /* store class in chain */
641 chain_addlast(nativeclass_chain, c);
643 /* open headerfile for class */
644 gen_header_filename(classname, c->name);
646 /* create chain for renaming fields */
647 ident_chain = chain_new();
650 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
653 sprintf(header_filename, "%s.h", classname);
656 file = fopen(header_filename, "w");
658 log_text("Can not open file to store header information");
662 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
664 /* convert to uppercase */
665 for (i = 0; classname[i]; i++) {
666 uclassname[i] = toupper(classname[i]);
668 uclassname[i] = '\0';
670 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
672 /* create structure for direct access to objects */
673 fprintf(file, "/* Structure information for class: ");
674 utf_fprint(file, c->name);
675 fprintf(file, " */\n\n");
676 fprintf(file, "typedef struct ");
678 fprintf(file, " {\n");
686 fprintf(file, ";\n\n");
688 /* create chain for renaming overloaded methods */
689 chain_free(ident_chain);
690 ident_chain = chain_new();
692 /* create method-prototypes */
694 /* find overloaded methods */
695 for (i = 0; i < c->methodscount; i++) {
697 m = &(c->methods[i]);
699 if (!(m->flags & ACC_NATIVE)) continue;
700 if (!m->nativelyoverloaded) {
701 nativelyoverloaded=false;
702 for (i2=i+1;i2<c->methodscount; i2++) {
703 m2 = &(c->methods[i2]);
704 if (!(m2->flags & ACC_NATIVE)) continue;
705 if (m->name==m2->name) {
706 m2->nativelyoverloaded=true;
707 nativelyoverloaded=true;
710 m->nativelyoverloaded=nativelyoverloaded;
715 for (i = 0; i < c->methodscount; i++) {
717 m = &(c->methods[i]);
719 if (m->flags & ACC_NATIVE) {
720 chain_addlast(nativemethod_chain, m);
725 chain_free(ident_chain);
727 fprintf(file, "#endif\n\n");
733 /******** print classname, '$' used to seperate inner-class name ***********/
735 void print_classname(classinfo *clazz)
737 utf *u = clazz->name;
738 char *endpos = u->text + u->blength;
739 char *utf_ptr = u->text;
742 while (utf_ptr < endpos) {
743 if ((c = utf_nextu2(&utf_ptr)) == '_')
752 * These are local overrides for various environment variables in Emacs.
753 * Please do not remove this and leave it at the end of the file, where
754 * Emacs will automagically detect them.
755 * ---------------------------------------------------------------------
758 * indent-tabs-mode: t