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 2518 2005-05-23 11:57:59Z twisti $
45 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
46 # if defined(__DARWIN__)
49 # include <ucontext.h>
53 #include "mm/memory.h"
54 #include "native/include/java_lang_String.h"
55 #include "native/include/java_lang_Throwable.h"
56 #include "toolbox/chain.h"
57 #include "toolbox/logging.h"
58 #include "vm/builtin.h"
60 #include "vm/global.h"
61 #include "vm/method.h"
62 #include "vm/tables.h"
63 #include "vm/loader.h"
64 #include "vm/options.h"
65 #include "vm/stringlocal.h"
66 #include "vm/jit/asmpart.h"
69 /******* replace some external functions *********/
71 functionptr native_findfunction(utf *cname, utf *mname, utf *desc, bool isstatic)
74 java_objectheader *native_new_and_init(classinfo *c) { return NULL; }
75 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s) { return NULL; }
76 java_objectheader *native_new_and_init_int(classinfo *c, s4 i) { return NULL; }
77 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t) { return NULL; }
80 java_objectheader *asm_calljavafunction(methodinfo *m, void *arg1, void *arg2,
81 void *arg3, void *arg4) { return NULL; }
83 void asm_call_jit_compiler(void) {}
85 /* code patching functions */
86 void patcher_builtin_new(u1 *sp) {}
87 void patcher_builtin_newarray(u1 *sp) {}
88 void patcher_builtin_arraycheckcast(u1 *sp) {}
89 void patcher_builtin_arrayinstanceof(u1 *sp) {}
91 s4 asm_builtin_idiv(s4 a, s4 b) { return 0; }
92 s4 asm_builtin_irem(s4 a, s4 b) { return 0; }
93 s8 asm_builtin_ldiv(s8 a, s8 b) { return 0; }
94 s8 asm_builtin_lrem(s8 a, s8 b) { return 0; }
96 s4 asm_builtin_f2i(float a) { return 0; }
97 s8 asm_builtin_f2l(float a) { return 0; }
98 s4 asm_builtin_d2i(double a) { return 0; }
99 s8 asm_builtin_d2l(double a) { return 0; }
101 void use_class_as_object(void) {}
102 void asm_builtin_monitorenter(java_objectheader *o) {}
103 void *asm_builtin_monitorexit(java_objectheader *o) { return NULL; }
105 s4 asm_builtin_arraycheckcast(java_objectheader *obj, vftbl_t *target)
110 #if defined(__MIPS__)
111 long compare_and_swap(long *p, long oldval, long newval)
122 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
124 u1 *createcompilerstub(methodinfo *m) { return NULL; }
125 u1 *createnativestub(functionptr f, methodinfo *m) { return NULL; }
126 u1 *oldcreatenativestub(functionptr f, methodinfo *m) { return NULL; }
128 void removecompilerstub(u1 *stub) {}
129 void removenativestub(u1 *stub) {}
131 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
132 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
134 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
136 void asm_handle_builtin_exception(classinfo *c) {}
137 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
139 #if defined(__DARWIN__)
140 int cacao_catch_Handler(void) {}
143 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
144 threadcritnode asm_criticalsections;
145 void thread_restartcriticalsection(ucontext_t *uc) {}
149 void setVMClassField(classinfo *c) {}
150 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
156 typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
162 /* exception functions ********************************************************/
164 /* these should not be called */
166 void throw_exception(void) { assert(0); }
167 void throw_exception_exit(void) { assert(0); }
169 java_objectheader *new_verifyerror(methodinfo *m, const char *message)
173 /* keep compiler happy */
178 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
182 /* keep compiler happy */
188 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
192 fprintf(stderr, "%s: ", exception);
194 va_start(ap, message);
195 vfprintf(stderr, message, ap);
202 java_objectheader *new_exception(const char *classname)
204 fprintf(stderr, "%s\n", classname);
207 /* keep compiler happy */
213 java_objectheader *new_exception_message(const char *classname, const char *message)
215 fprintf(stderr, "%s: %s\n", classname, message);
218 /* keep compiler happy */
224 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
226 fprintf(stderr, "%s: ", classname);
227 utf_display(message);
232 /* keep compiler happy */
238 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
242 utf_display(c->name);
243 fprintf(stderr, ": ");
245 va_start(ap, message);
246 vfprintf(stderr, message, ap);
253 /* keep compiler happy */
259 java_objectheader *new_internalerror(const char *message, ...)
263 fprintf(stderr, "%s: ", string_java_lang_InternalError);
265 va_start(ap, message);
266 vfprintf(stderr, message, ap);
271 /* keep compiler happy */
277 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
281 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
283 utf_display(c->name);
284 fprintf(stderr, ": ");
286 va_start(ap, message);
287 vfprintf(stderr, message, ap);
292 /* keep compiler happy */
298 java_objectheader *new_negativearraysizeexception(void)
300 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
303 /* keep compiler happy */
309 java_objectheader *new_nullpointerexception(void)
311 fprintf(stderr, "%s", string_java_lang_NullPointerException);
314 /* keep compiler happy */
320 /************************ global variables **********************/
322 chain *nativemethod_chain; /* chain with native methods */
323 chain *nativeclass_chain; /* chain with processed classes */
324 chain *ident_chain; /* chain with method and field names in current class */
326 static u4 outputsize;
327 static bool dopadding;
330 static void printIDpart(int c)
332 if ((c >= 'a' && c <= 'z') ||
333 (c >= 'A' && c <= 'Z') ||
334 (c >= '0' && c <= '9') ||
344 char *utf_ptr = u->text;
347 for (i = 0; i < utf_strlen(u); i++)
348 printIDpart(utf_nextu2(&utf_ptr));
352 static void addoutputsize (int len)
355 if (!dopadding) return;
357 newsize = ALIGN(outputsize, len);
359 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
360 outputsize = newsize;
364 void printOverloadPart(utf *desc)
366 char *utf_ptr=desc->text;
371 while ((c = utf_nextu2(&utf_ptr)) != ')') {
381 fprintf(file, "%c", (char) c);
388 while ((c = utf_nextu2(&utf_ptr)) != ';')
395 log_text("invalid method descriptor");
401 static char *printtype(char *utf_ptr)
405 switch (utf_nextu2(&utf_ptr)) {
406 case 'V': fprintf (file, "void");
412 case 'Z': addoutputsize (4);
413 fprintf (file, "s4");
415 case 'J': addoutputsize (8);
416 fprintf (file, "s8");
418 case 'F': addoutputsize (4);
419 fprintf (file, "float");
421 case 'D': addoutputsize (8);
422 fprintf (file, "double");
425 addoutputsize ( sizeof(java_arrayheader*) );
426 switch (utf_nextu2(&utf_ptr)) {
427 case 'I': fprintf (file, "java_intarray*"); break;
428 case 'J': fprintf (file, "java_longarray*"); break;
429 case 'Z': fprintf (file, "java_booleanarray*"); break;
430 case 'B': fprintf (file, "java_bytearray*"); break;
431 case 'S': fprintf (file, "java_shortarray*"); break;
432 case 'C': fprintf (file, "java_chararray*"); break;
433 case 'F': fprintf (file, "java_floatarray*"); break;
434 case 'D': fprintf (file, "java_doublearray*"); break;
436 case '[': fprintf(file, "java_objectarray*");
437 while ((c = utf_nextu2(&utf_ptr)) == '[');
439 while (utf_nextu2(&utf_ptr) != ';');
442 case 'L': fprintf(file, "java_objectarray*");
443 while (utf_nextu2(&utf_ptr) != ';');
446 log_text("invalid type descriptor");
452 addoutputsize ( sizeof(java_objectheader*));
453 fprintf (file, "struct ");
454 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
459 log_text("Unknown type in field descriptor");
467 /***** determine the number of entries of a utf string in the ident chain *****/
469 static int searchidentchain_utf(utf *ident)
471 utf *u = chain_first(ident_chain); /* first element of list */
475 if (u==ident) count++; /* string found */
476 u = chain_next(ident_chain); /* next element in list */
483 /************** print structure for direct access to objects ******************/
485 static void printfields(classinfo *c)
492 addoutputsize(sizeof(java_objectheader));
493 fprintf(file, " java_objectheader header;\n");
497 printfields(c->super.cls);
499 for (i = 0; i < c->fieldscount; i++) {
502 if (!(f->flags & ACC_STATIC)) {
504 printtype(f->descriptor->text);
506 utf_fprint(file, f->name);
508 /* rename multiple fieldnames */
509 if ((ident_count = searchidentchain_utf(f->name)))
510 fprintf(file, "%d", ident_count - 1);
511 chain_addlast(ident_chain, f->name);
513 fprintf(file, ";\n");
519 /***************** store prototype for native method in file ******************/
521 void printmethod(methodinfo *m)
526 /* search for return-type in descriptor */
527 utf_ptr = m->descriptor->text;
528 while (utf_nextu2(&utf_ptr) != ')');
531 fprintf(file, "\n/*\n * Class: ");
532 utf_fprint(file, m->class->name);
533 fprintf(file, "\n * Method: ");
534 utf_fprint(file, m->name);
535 fprintf(file, "\n * Signature: ");
536 utf_fprint(file, m->descriptor);
537 fprintf(file, "\n */\n");
539 /* create prototype */
540 fprintf(file, "JNIEXPORT ");
542 fprintf(file, " JNICALL Java_");
543 printID(m->class->name);
545 chain_addlast(ident_chain, m->name);
549 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
550 fprintf(file, "(JNIEnv *env");
552 utf_ptr = m->descriptor->text + 1;
554 if (!(m->flags & ACC_STATIC)) {
555 fprintf(file, ", struct ");
556 printID(m->class->name);
557 fprintf(file, "* this");
560 fprintf(file, ", jclass clazz");
563 if ((*utf_ptr) != ')') fprintf(file, ", ");
565 while ((*utf_ptr) != ')') {
566 utf_ptr = printtype(utf_ptr);
567 fprintf(file, " par%d", paramnum++);
568 if ((*utf_ptr)!=')') fprintf(file, ", ");
571 fprintf(file, ");\n\n");
575 /******* remove package-name in fully-qualified classname *********************/
577 void gen_header_filename(char *buffer, utf *u)
581 for (i = 0; i < utf_strlen(u); i++) {
582 if ((u->text[i] == '/') || (u->text[i] == '$')) {
583 buffer[i] = '_'; /* convert '$' and '/' to '_' */
586 buffer[i] = u->text[i];
589 buffer[utf_strlen(u)] = '\0';
593 /* create headerfile for classes and store native methods in chain ************/
595 void headerfile_generate(classinfo *c, char *opt_directory)
597 char header_filename[1024] = "";
598 char classname[1024];
599 char uclassname[1024];
604 u2 nativelyoverloaded;
606 /* store class in chain */
607 chain_addlast(nativeclass_chain, c);
609 /* open headerfile for class */
610 gen_header_filename(classname, c->name);
612 /* create chain for renaming fields */
613 ident_chain = chain_new();
616 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
619 sprintf(header_filename, "%s.h", classname);
622 file = fopen(header_filename, "w");
624 log_text("Can not open file to store header information");
628 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
630 /* convert to uppercase */
631 for (i = 0; classname[i]; i++) {
632 uclassname[i] = toupper(classname[i]);
634 uclassname[i] = '\0';
636 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
638 /* create structure for direct access to objects */
639 fprintf(file, "/* Structure information for class: ");
640 utf_fprint(file, c->name);
641 fprintf(file, " */\n\n");
642 fprintf(file, "typedef struct ");
644 fprintf(file, " {\n");
652 fprintf(file, ";\n\n");
654 /* create chain for renaming overloaded methods */
655 chain_free(ident_chain);
656 ident_chain = chain_new();
658 /* create method-prototypes */
660 /* find overloaded methods */
661 for (i = 0; i < c->methodscount; i++) {
663 m = &(c->methods[i]);
665 if (!(m->flags & ACC_NATIVE)) continue;
666 if (!m->nativelyoverloaded) {
667 nativelyoverloaded=false;
668 for (i2=i+1;i2<c->methodscount; i2++) {
669 m2 = &(c->methods[i2]);
670 if (!(m2->flags & ACC_NATIVE)) continue;
671 if (m->name==m2->name) {
672 m2->nativelyoverloaded=true;
673 nativelyoverloaded=true;
676 m->nativelyoverloaded=nativelyoverloaded;
681 for (i = 0; i < c->methodscount; i++) {
683 m = &(c->methods[i]);
685 if (m->flags & ACC_NATIVE) {
686 chain_addlast(nativemethod_chain, m);
691 chain_free(ident_chain);
693 fprintf(file, "#endif\n\n");
699 /******** print classname, '$' used to seperate inner-class name ***********/
701 void print_classname(classinfo *clazz)
703 utf *u = clazz->name;
704 char *endpos = u->text + u->blength;
705 char *utf_ptr = u->text;
708 while (utf_ptr < endpos) {
709 if ((c = utf_nextu2(&utf_ptr)) == '_')
718 * These are local overrides for various environment variables in Emacs.
719 * Please do not remove this and leave it at the end of the file, where
720 * Emacs will automagically detect them.
721 * ---------------------------------------------------------------------
724 * indent-tabs-mode: t