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 2551 2005-06-06 14:44:56Z 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 u1 *createcompilerstub(methodinfo *m) { return NULL; }
133 u1 *createnativestub(functionptr f, methodinfo *m) { return NULL; }
134 u1 *oldcreatenativestub(functionptr f, methodinfo *m) { return NULL; }
136 void removecompilerstub(u1 *stub) {}
137 void removenativestub(u1 *stub) {}
139 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
140 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
142 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
144 void asm_handle_builtin_exception(classinfo *c) {}
145 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
148 void setVMClassField(classinfo *c) {}
149 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
155 typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
161 /* exception functions ********************************************************/
163 /* these should not be called */
165 void throw_exception(void) { assert(0); }
166 void throw_exception_exit(void) { assert(0); }
168 java_objectheader *new_verifyerror(methodinfo *m, const char *message)
172 /* keep compiler happy */
177 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
181 /* keep compiler happy */
187 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
191 fprintf(stderr, "%s: ", exception);
193 va_start(ap, message);
194 vfprintf(stderr, message, ap);
201 java_objectheader *new_exception(const char *classname)
203 fprintf(stderr, "%s\n", classname);
206 /* keep compiler happy */
212 java_objectheader *new_exception_message(const char *classname, const char *message)
214 fprintf(stderr, "%s: %s\n", classname, message);
217 /* keep compiler happy */
223 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
225 fprintf(stderr, "%s: ", classname);
226 utf_display(message);
231 /* keep compiler happy */
237 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
241 utf_display(c->name);
242 fprintf(stderr, ": ");
244 va_start(ap, message);
245 vfprintf(stderr, message, ap);
252 /* keep compiler happy */
258 java_objectheader *new_internalerror(const char *message, ...)
262 fprintf(stderr, "%s: ", string_java_lang_InternalError);
264 va_start(ap, message);
265 vfprintf(stderr, message, ap);
270 /* keep compiler happy */
276 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
280 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
282 utf_display(c->name);
283 fprintf(stderr, ": ");
285 va_start(ap, message);
286 vfprintf(stderr, message, ap);
291 /* keep compiler happy */
297 java_objectheader *new_negativearraysizeexception(void)
299 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
302 /* keep compiler happy */
308 java_objectheader *new_nullpointerexception(void)
310 fprintf(stderr, "%s", string_java_lang_NullPointerException);
313 /* keep compiler happy */
319 /* machine dependent stuff ****************************************************/
321 #if defined(__DARWIN__)
322 int cacao_catch_Handler(void) {}
325 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
326 threadcritnode asm_criticalsections;
327 void thread_restartcriticalsection(ucontext_t *uc) {}
330 void md_param_alloc(methoddesc *md) {}
333 /************************ global variables **********************/
335 chain *nativemethod_chain; /* chain with native methods */
336 chain *nativeclass_chain; /* chain with processed classes */
337 chain *ident_chain; /* chain with method and field names in current class */
339 static u4 outputsize;
340 static bool dopadding;
343 static void printIDpart(int c)
345 if ((c >= 'a' && c <= 'z') ||
346 (c >= 'A' && c <= 'Z') ||
347 (c >= '0' && c <= '9') ||
357 char *utf_ptr = u->text;
360 for (i = 0; i < utf_strlen(u); i++)
361 printIDpart(utf_nextu2(&utf_ptr));
365 static void addoutputsize (int len)
368 if (!dopadding) return;
370 newsize = ALIGN(outputsize, len);
372 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
373 outputsize = newsize;
377 void printOverloadPart(utf *desc)
379 char *utf_ptr=desc->text;
384 while ((c = utf_nextu2(&utf_ptr)) != ')') {
394 fprintf(file, "%c", (char) c);
401 while ((c = utf_nextu2(&utf_ptr)) != ';')
408 log_text("invalid method descriptor");
414 static char *printtype(char *utf_ptr)
418 switch (utf_nextu2(&utf_ptr)) {
419 case 'V': fprintf (file, "void");
425 case 'Z': addoutputsize (4);
426 fprintf (file, "s4");
428 case 'J': addoutputsize (8);
429 fprintf (file, "s8");
431 case 'F': addoutputsize (4);
432 fprintf (file, "float");
434 case 'D': addoutputsize (8);
435 fprintf (file, "double");
438 addoutputsize ( sizeof(java_arrayheader*) );
439 switch (utf_nextu2(&utf_ptr)) {
440 case 'I': fprintf (file, "java_intarray*"); break;
441 case 'J': fprintf (file, "java_longarray*"); break;
442 case 'Z': fprintf (file, "java_booleanarray*"); break;
443 case 'B': fprintf (file, "java_bytearray*"); break;
444 case 'S': fprintf (file, "java_shortarray*"); break;
445 case 'C': fprintf (file, "java_chararray*"); break;
446 case 'F': fprintf (file, "java_floatarray*"); break;
447 case 'D': fprintf (file, "java_doublearray*"); break;
449 case '[': fprintf(file, "java_objectarray*");
450 while ((c = utf_nextu2(&utf_ptr)) == '[');
452 while (utf_nextu2(&utf_ptr) != ';');
455 case 'L': fprintf(file, "java_objectarray*");
456 while (utf_nextu2(&utf_ptr) != ';');
459 log_text("invalid type descriptor");
465 addoutputsize ( sizeof(java_objectheader*));
466 fprintf (file, "struct ");
467 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
472 log_text("Unknown type in field descriptor");
480 /***** determine the number of entries of a utf string in the ident chain *****/
482 static int searchidentchain_utf(utf *ident)
484 utf *u = chain_first(ident_chain); /* first element of list */
488 if (u==ident) count++; /* string found */
489 u = chain_next(ident_chain); /* next element in list */
496 /************** print structure for direct access to objects ******************/
498 static void printfields(classinfo *c)
505 addoutputsize(sizeof(java_objectheader));
506 fprintf(file, " java_objectheader header;\n");
510 printfields(c->super.cls);
512 for (i = 0; i < c->fieldscount; i++) {
515 if (!(f->flags & ACC_STATIC)) {
517 printtype(f->descriptor->text);
519 utf_fprint(file, f->name);
521 /* rename multiple fieldnames */
522 if ((ident_count = searchidentchain_utf(f->name)))
523 fprintf(file, "%d", ident_count - 1);
524 chain_addlast(ident_chain, f->name);
526 fprintf(file, ";\n");
532 /***************** store prototype for native method in file ******************/
534 void printmethod(methodinfo *m)
539 /* search for return-type in descriptor */
540 utf_ptr = m->descriptor->text;
541 while (utf_nextu2(&utf_ptr) != ')');
544 fprintf(file, "\n/*\n * Class: ");
545 utf_fprint(file, m->class->name);
546 fprintf(file, "\n * Method: ");
547 utf_fprint(file, m->name);
548 fprintf(file, "\n * Signature: ");
549 utf_fprint(file, m->descriptor);
550 fprintf(file, "\n */\n");
552 /* create prototype */
553 fprintf(file, "JNIEXPORT ");
555 fprintf(file, " JNICALL Java_");
556 printID(m->class->name);
558 chain_addlast(ident_chain, m->name);
562 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
563 fprintf(file, "(JNIEnv *env");
565 utf_ptr = m->descriptor->text + 1;
567 if (!(m->flags & ACC_STATIC)) {
568 fprintf(file, ", struct ");
569 printID(m->class->name);
570 fprintf(file, "* this");
573 fprintf(file, ", jclass clazz");
576 if ((*utf_ptr) != ')') fprintf(file, ", ");
578 while ((*utf_ptr) != ')') {
579 utf_ptr = printtype(utf_ptr);
580 fprintf(file, " par%d", paramnum++);
581 if ((*utf_ptr)!=')') fprintf(file, ", ");
584 fprintf(file, ");\n\n");
588 /******* remove package-name in fully-qualified classname *********************/
590 void gen_header_filename(char *buffer, utf *u)
594 for (i = 0; i < utf_strlen(u); i++) {
595 if ((u->text[i] == '/') || (u->text[i] == '$')) {
596 buffer[i] = '_'; /* convert '$' and '/' to '_' */
599 buffer[i] = u->text[i];
602 buffer[utf_strlen(u)] = '\0';
606 /* create headerfile for classes and store native methods in chain ************/
608 void headerfile_generate(classinfo *c, char *opt_directory)
610 char header_filename[1024] = "";
611 char classname[1024];
612 char uclassname[1024];
617 u2 nativelyoverloaded;
619 /* store class in chain */
620 chain_addlast(nativeclass_chain, c);
622 /* open headerfile for class */
623 gen_header_filename(classname, c->name);
625 /* create chain for renaming fields */
626 ident_chain = chain_new();
629 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
632 sprintf(header_filename, "%s.h", classname);
635 file = fopen(header_filename, "w");
637 log_text("Can not open file to store header information");
641 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
643 /* convert to uppercase */
644 for (i = 0; classname[i]; i++) {
645 uclassname[i] = toupper(classname[i]);
647 uclassname[i] = '\0';
649 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
651 /* create structure for direct access to objects */
652 fprintf(file, "/* Structure information for class: ");
653 utf_fprint(file, c->name);
654 fprintf(file, " */\n\n");
655 fprintf(file, "typedef struct ");
657 fprintf(file, " {\n");
665 fprintf(file, ";\n\n");
667 /* create chain for renaming overloaded methods */
668 chain_free(ident_chain);
669 ident_chain = chain_new();
671 /* create method-prototypes */
673 /* find overloaded methods */
674 for (i = 0; i < c->methodscount; i++) {
676 m = &(c->methods[i]);
678 if (!(m->flags & ACC_NATIVE)) continue;
679 if (!m->nativelyoverloaded) {
680 nativelyoverloaded=false;
681 for (i2=i+1;i2<c->methodscount; i2++) {
682 m2 = &(c->methods[i2]);
683 if (!(m2->flags & ACC_NATIVE)) continue;
684 if (m->name==m2->name) {
685 m2->nativelyoverloaded=true;
686 nativelyoverloaded=true;
689 m->nativelyoverloaded=nativelyoverloaded;
694 for (i = 0; i < c->methodscount; i++) {
696 m = &(c->methods[i]);
698 if (m->flags & ACC_NATIVE) {
699 chain_addlast(nativemethod_chain, m);
704 chain_free(ident_chain);
706 fprintf(file, "#endif\n\n");
712 /******** print classname, '$' used to seperate inner-class name ***********/
714 void print_classname(classinfo *clazz)
716 utf *u = clazz->name;
717 char *endpos = u->text + u->blength;
718 char *utf_ptr = u->text;
721 while (utf_ptr < endpos) {
722 if ((c = utf_nextu2(&utf_ptr)) == '_')
731 * These are local overrides for various environment variables in Emacs.
732 * Please do not remove this and leave it at the end of the file, where
733 * Emacs will automagically detect them.
734 * ---------------------------------------------------------------------
737 * indent-tabs-mode: t