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 2464 2005-05-12 23:55:10Z 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); }
168 java_objectheader *new_verifyerror(methodinfo *m, const char *message) { assert(0); }
169 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable) { assert(0); }
172 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
176 fprintf(stderr, "%s: ", exception);
178 va_start(ap, message);
179 vfprintf(stderr, message, ap);
186 java_objectheader *new_exception(const char *classname)
188 fprintf(stderr, "%s\n", classname);
193 java_objectheader *new_exception_message(const char *classname, const char *message)
195 fprintf(stderr, "%s: %s\n", classname, message);
200 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
202 fprintf(stderr, "%s: ", classname);
203 utf_display(message);
210 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
214 utf_display(c->name);
215 fprintf(stderr, ": ");
217 va_start(ap, message);
218 vfprintf(stderr, message, ap);
227 java_objectheader *new_internalerror(const char *message, ...)
231 fprintf(stderr, "%s: ", string_java_lang_InternalError);
233 va_start(ap, message);
234 vfprintf(stderr, message, ap);
241 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
245 fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
247 utf_display(c->name);
248 fprintf(stderr, ": ");
250 va_start(ap, message);
251 vfprintf(stderr, message, ap);
258 java_objectheader *new_negativearraysizeexception(void)
260 fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
265 java_objectheader *new_nullpointerexception(void)
267 fprintf(stderr, "%s", string_java_lang_NullPointerException);
272 /************************ global variables **********************/
274 chain *nativemethod_chain; /* chain with native methods */
275 chain *nativeclass_chain; /* chain with processed classes */
276 chain *ident_chain; /* chain with method and field names in current class */
278 static u4 outputsize;
279 static bool dopadding;
282 static void printIDpart(int c)
284 if ((c >= 'a' && c <= 'z') ||
285 (c >= 'A' && c <= 'Z') ||
286 (c >= '0' && c <= '9') ||
296 char *utf_ptr = u->text;
299 for (i = 0; i < utf_strlen(u); i++)
300 printIDpart(utf_nextu2(&utf_ptr));
304 static void addoutputsize (int len)
307 if (!dopadding) return;
309 newsize = ALIGN(outputsize, len);
311 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
312 outputsize = newsize;
316 void printOverloadPart(utf *desc)
318 char *utf_ptr=desc->text;
323 while ((c = utf_nextu2(&utf_ptr)) != ')') {
333 fprintf(file, "%c", (char) c);
340 while ((c = utf_nextu2(&utf_ptr)) != ';')
347 log_text("invalid method descriptor");
353 static char *printtype(char *utf_ptr)
357 switch (utf_nextu2(&utf_ptr)) {
358 case 'V': fprintf (file, "void");
364 case 'Z': addoutputsize (4);
365 fprintf (file, "s4");
367 case 'J': addoutputsize (8);
368 fprintf (file, "s8");
370 case 'F': addoutputsize (4);
371 fprintf (file, "float");
373 case 'D': addoutputsize (8);
374 fprintf (file, "double");
377 addoutputsize ( sizeof(java_arrayheader*) );
378 switch (utf_nextu2(&utf_ptr)) {
379 case 'I': fprintf (file, "java_intarray*"); break;
380 case 'J': fprintf (file, "java_longarray*"); break;
381 case 'Z': fprintf (file, "java_booleanarray*"); break;
382 case 'B': fprintf (file, "java_bytearray*"); break;
383 case 'S': fprintf (file, "java_shortarray*"); break;
384 case 'C': fprintf (file, "java_chararray*"); break;
385 case 'F': fprintf (file, "java_floatarray*"); break;
386 case 'D': fprintf (file, "java_doublearray*"); break;
388 case '[': fprintf (file, "java_objectarray*");
389 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
391 while (utf_nextu2(&utf_ptr) != ';');
394 case 'L': fprintf (file, "java_objectarray*");
395 while ( utf_nextu2(&utf_ptr) != ';');
397 default: panic ("invalid type descriptor");
402 addoutputsize ( sizeof(java_objectheader*));
403 fprintf (file, "struct ");
404 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
408 default: panic ("Unknown type in field descriptor");
415 /***** determine the number of entries of a utf string in the ident chain *****/
417 static int searchidentchain_utf(utf *ident)
419 utf *u = chain_first(ident_chain); /* first element of list */
423 if (u==ident) count++; /* string found */
424 u = chain_next(ident_chain); /* next element in list */
431 /************** print structure for direct access to objects ******************/
433 static void printfields(classinfo *c)
440 addoutputsize(sizeof(java_objectheader));
441 fprintf(file, " java_objectheader header;\n");
445 printfields(c->super.cls);
447 for (i = 0; i < c->fieldscount; i++) {
450 if (!(f->flags & ACC_STATIC)) {
452 printtype(f->descriptor->text);
454 utf_fprint(file, f->name);
456 /* rename multiple fieldnames */
457 if ((ident_count = searchidentchain_utf(f->name)))
458 fprintf(file, "%d", ident_count - 1);
459 chain_addlast(ident_chain, f->name);
461 fprintf(file, ";\n");
467 /***************** store prototype for native method in file ******************/
469 void printmethod(methodinfo *m)
474 /* search for return-type in descriptor */
475 utf_ptr = m->descriptor->text;
476 while (utf_nextu2(&utf_ptr) != ')');
479 fprintf(file, "\n/*\n * Class: ");
480 utf_fprint(file, m->class->name);
481 fprintf(file, "\n * Method: ");
482 utf_fprint(file, m->name);
483 fprintf(file, "\n * Signature: ");
484 utf_fprint(file, m->descriptor);
485 fprintf(file, "\n */\n");
487 /* create prototype */
488 fprintf(file, "JNIEXPORT ");
490 fprintf(file, " JNICALL Java_");
491 printID(m->class->name);
493 chain_addlast(ident_chain, m->name);
497 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
498 fprintf(file, "(JNIEnv *env");
500 utf_ptr = m->descriptor->text + 1;
502 if (!(m->flags & ACC_STATIC)) {
503 fprintf(file, ", struct ");
504 printID(m->class->name);
505 fprintf(file, "* this");
508 fprintf(file, ", jclass clazz");
511 if ((*utf_ptr) != ')') fprintf(file, ", ");
513 while ((*utf_ptr) != ')') {
514 utf_ptr = printtype(utf_ptr);
515 fprintf(file, " par%d", paramnum++);
516 if ((*utf_ptr)!=')') fprintf(file, ", ");
519 fprintf(file, ");\n\n");
523 /******* remove package-name in fully-qualified classname *********************/
525 void gen_header_filename(char *buffer, utf *u)
529 for (i = 0; i < utf_strlen(u); i++) {
530 if ((u->text[i] == '/') || (u->text[i] == '$')) {
531 buffer[i] = '_'; /* convert '$' and '/' to '_' */
534 buffer[i] = u->text[i];
537 buffer[utf_strlen(u)] = '\0';
541 /* create headerfile for classes and store native methods in chain ************/
543 void headerfile_generate(classinfo *c, char *opt_directory)
545 char header_filename[1024] = "";
546 char classname[1024];
547 char uclassname[1024];
552 u2 nativelyoverloaded;
554 /* store class in chain */
555 chain_addlast(nativeclass_chain, c);
557 /* open headerfile for class */
558 gen_header_filename(classname, c->name);
560 /* create chain for renaming fields */
561 ident_chain = chain_new();
564 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
567 sprintf(header_filename, "%s.h", classname);
570 file = fopen(header_filename, "w");
572 panic("Can not open file to store header information");
574 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
576 /* convert to uppercase */
577 for (i = 0; classname[i]; i++) {
578 uclassname[i] = toupper(classname[i]);
580 uclassname[i] = '\0';
582 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
584 /* create structure for direct access to objects */
585 fprintf(file, "/* Structure information for class: ");
586 utf_fprint(file, c->name);
587 fprintf(file, " */\n\n");
588 fprintf(file, "typedef struct ");
590 fprintf(file, " {\n");
598 fprintf(file, ";\n\n");
600 /* create chain for renaming overloaded methods */
601 chain_free(ident_chain);
602 ident_chain = chain_new();
604 /* create method-prototypes */
606 /* find overloaded methods */
607 for (i = 0; i < c->methodscount; i++) {
609 m = &(c->methods[i]);
611 if (!(m->flags & ACC_NATIVE)) continue;
612 if (!m->nativelyoverloaded) {
613 nativelyoverloaded=false;
614 for (i2=i+1;i2<c->methodscount; i2++) {
615 m2 = &(c->methods[i2]);
616 if (!(m2->flags & ACC_NATIVE)) continue;
617 if (m->name==m2->name) {
618 m2->nativelyoverloaded=true;
619 nativelyoverloaded=true;
622 m->nativelyoverloaded=nativelyoverloaded;
627 for (i = 0; i < c->methodscount; i++) {
629 m = &(c->methods[i]);
631 if (m->flags & ACC_NATIVE) {
632 chain_addlast(nativemethod_chain, m);
637 chain_free(ident_chain);
639 fprintf(file, "#endif\n\n");
645 /******** print classname, '$' used to seperate inner-class name ***********/
647 void print_classname(classinfo *clazz)
649 utf *u = clazz->name;
650 char *endpos = u->text + u->blength;
651 char *utf_ptr = u->text;
654 while (utf_ptr < endpos) {
655 if ((c = utf_nextu2(&utf_ptr)) == '_')
664 * These are local overrides for various environment variables in Emacs.
665 * Please do not remove this and leave it at the end of the file, where
666 * Emacs will automagically detect them.
667 * ---------------------------------------------------------------------
670 * indent-tabs-mode: t