1 /* headers.c - functions for header generation
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5 M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6 P. Tomsich, J. Wenninger
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 1244 2004-06-30 20:14:20Z twisti $
48 #include "toolbox/chain.h"
49 #include "toolbox/memory.h"
50 #include "toolbox/logging.h"
51 #include "nat/java_lang_String.h"
54 /******* replace some external functions *********/
56 functionptr native_findfunction(utf *cname, utf *mname, utf *desc, bool isstatic)
59 java_objectheader *javastring_new(utf *text)
62 char *javastring_tochar(java_objectheader *so) { return NULL; }
63 utf *javastring_toutf(java_lang_String *string, bool isclassname)
67 /* some exception stuff */
69 classinfo *class_java_lang_Exception;
72 char *string_java_lang_IllegalMonitorStateException =
73 "java/lang/IllegalMonitorStateException";
75 char *string_java_lang_NegativeArraySizeException =
76 "java/lang/NegativeArraySizeException";
79 char *string_java_lang_AbstractMethodError =
80 "java/lang/AbstractMethodError";
82 char *string_java_lang_ClassCircularityError =
83 "java/lang/ClassCircularityError";
85 char *string_java_lang_ClassFormatError =
86 "java/lang/ClassFormatError";
88 char *string_java_lang_ExceptionInInitializerError =
89 "java/lang/ExceptionInInitializerError";
91 char *string_java_lang_IncompatibleClassChangeError =
92 "java/lang/IncompatibleClassChangeError";
94 char *string_java_lang_InternalError =
95 "java/lang/InternalError";
97 char *string_java_lang_LinkageError =
98 "java/lang/LinkageError";
100 char *string_java_lang_NoClassDefFoundError =
101 "java/lang/NoClassDefFoundError";
103 char *string_java_lang_NoSuchFieldError =
104 "java/lang/NoSuchFieldError";
106 char *string_java_lang_NoSuchMethodError =
107 "java/lang/NoSuchMethodError";
109 char *string_java_lang_OutOfMemoryError =
110 "java/lang/OutOfMemoryError";
113 void throw_exception() {}
114 void throw_exception_exit() {}
116 void throw_cacao_exception_exit(char *exception, char *message)
119 "Exception in thread \"main\" %s: %s\n", exception, message);
127 void new_exception(char *classname)
129 printf("Exception in thread \"main\" %s\n", classname);
133 void new_exception_message(char *classname, char *message)
135 printf("Exception in thread \"main\" %s: %s\n", classname, message);
139 void new_exception_throwable(char *classname, java_objectheader *t) {}
140 void new_exception_utfmessage(char *classname, utf *message)
142 printf("Exception in thread \"main\" %s: ", classname);
143 utf_display(message);
148 java_objectheader *literalstring_new(utf *u) { return NULL; }
151 void literalstring_free(java_objectheader *o) {}
152 void stringtable_update() { }
153 void synchronize_caches() { }
154 void asm_call_jit_compiler() { }
155 void asm_calljavafunction() { }
156 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
158 s4 asm_builtin_idiv(s4 a, s4 b) {return 0;}
159 s4 asm_builtin_irem(s4 a, s4 b) {return 0;}
160 s8 asm_builtin_ldiv(s8 a, s8 b) {return 0;}
161 s8 asm_builtin_lrem(s8 a, s8 b) {return 0;}
163 s4 asm_builtin_f2i(float a) { return 0; }
164 s8 asm_builtin_f2l(float a) { return 0; }
165 s4 asm_builtin_d2i(double a) { return 0; }
166 s8 asm_builtin_d2l(double a) { return 0; }
168 void asm_builtin_monitorenter(java_objectheader *o) {}
169 void asm_builtin_monitorexit(java_objectheader *o) {}
171 s4 asm_builtin_checkarraycast(java_objectheader *obj, vftbl *target) {return 0;}
173 #if defined(__MIPS__)
174 void atomic_swap() {}
175 void compare_and_swap() {}
179 #if defined(__I386__)
180 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) { return 0; }
181 void asm_builtin_newarray(s4 size, vftbl *arrayvftbl) {}
184 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
186 u1 *createcompilerstub(methodinfo *m) {return NULL;}
187 u1 *createnativestub(functionptr f, methodinfo *m) {return NULL;}
188 u1 *oldcreatenativestub(functionptr f, methodinfo *m) {return NULL;}
190 void removecompilerstub(u1 *stub) {}
191 void removenativestub(u1 *stub) {}
193 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
194 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
195 void thread_restartcriticalsection() {}
196 void asm_switchstackandcall() {}
197 void asm_handle_builtin_exception(classinfo *c) {}
198 void asm_getclassvalues_atomic() {}
200 #if defined(__DARWIN__)
201 int cacao_catch_Handler() {}
204 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
205 threadcritnode asm_criticalsections;
208 java_objectheader *native_new_and_init(void *p) { return NULL; }
211 /************************ global variables **********************/
213 THREADSPECIFIC java_objectheader *_exceptionptr;
215 chain *nativemethod_chain; /* chain with native methods */
216 chain *nativeclass_chain; /* chain with processed classes */
217 static chain *ident_chain; /* chain with method and field names in current class */
219 static u4 outputsize;
220 static bool dopadding;
223 static void printIDpart(int c)
225 if ((c >= 'a' && c <= 'z')
226 || (c >= 'A' && c <= 'Z')
227 || (c >= '0' && c <= '9')
235 static void printID(utf *u)
237 char *utf_ptr = u->text;
240 for (i = 0; i < utf_strlen(u); i++)
241 printIDpart(utf_nextu2(&utf_ptr));
245 static void addoutputsize (int len)
248 if (!dopadding) return;
250 newsize = ALIGN(outputsize, len);
252 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
253 outputsize = newsize;
256 static void printOverloadPart(utf *desc)
258 char *utf_ptr=desc->text;
262 while ((c=utf_nextu2(&utf_ptr))!=')') {
272 fprintf (file, "%c",(char)c);
279 while ( (c=utf_nextu2(&utf_ptr)) != ';')
285 default: panic ("invalid method descriptor");
290 static char *printtype(char *utf_ptr)
294 switch (utf_nextu2(&utf_ptr)) {
295 case 'V': fprintf (file, "void");
301 case 'Z': addoutputsize (4);
302 fprintf (file, "s4");
304 case 'J': addoutputsize (8);
305 fprintf (file, "s8");
307 case 'F': addoutputsize (4);
308 fprintf (file, "float");
310 case 'D': addoutputsize (8);
311 fprintf (file, "double");
314 addoutputsize ( sizeof(java_arrayheader*) );
315 switch (utf_nextu2(&utf_ptr)) {
316 case 'I': fprintf (file, "java_intarray*"); break;
317 case 'J': fprintf (file, "java_longarray*"); break;
318 case 'Z': fprintf (file, "java_booleanarray*"); break;
319 case 'B': fprintf (file, "java_bytearray*"); break;
320 case 'S': fprintf (file, "java_shortarray*"); break;
321 case 'C': fprintf (file, "java_chararray*"); break;
322 case 'F': fprintf (file, "java_floatarray*"); break;
323 case 'D': fprintf (file, "java_doublearray*"); break;
325 case '[': fprintf (file, "java_objectarray*");
326 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
328 while (utf_nextu2(&utf_ptr) != ';');
331 case 'L': fprintf (file, "java_objectarray*");
332 while ( utf_nextu2(&utf_ptr) != ';');
334 default: panic ("invalid type descriptor");
339 addoutputsize ( sizeof(java_objectheader*));
340 fprintf (file, "struct ");
341 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
345 default: panic ("Unknown type in field descriptor");
352 /***** determine the number of entries of a utf string in the ident chain *****/
354 static int searchidentchain_utf(utf *ident)
356 utf *u = chain_first(ident_chain); /* first element of list */
360 if (u==ident) count++; /* string found */
361 u = chain_next(ident_chain); /* next element in list */
368 /************** print structure for direct access to objects ******************/
370 static void printfields(classinfo *c)
377 addoutputsize(sizeof(java_objectheader));
378 fprintf(file, " java_objectheader header;\n");
382 printfields(c->super);
384 for (i = 0; i < c->fieldscount; i++) {
387 if (!(f->flags & ACC_STATIC)) {
389 printtype(f->descriptor->text);
391 utf_fprint(file, f->name);
393 /* rename multiple fieldnames */
394 if ((ident_count = searchidentchain_utf(f->name)))
395 fprintf(file, "%d", ident_count - 1);
396 chain_addlast(ident_chain, f->name);
398 fprintf(file, ";\n");
404 /***************** store prototype for native method in file ******************/
406 void printmethod(methodinfo *m)
412 /* search for return-type in descriptor */
413 utf_ptr = m->descriptor->text;
414 while (utf_nextu2(&utf_ptr) != ')');
417 fprintf(file, "\n/*\n * Class: ");
418 utf_fprint(file, m->class->name);
419 fprintf(file, "\n * Method: ");
420 utf_fprint(file, m->name);
421 fprintf(file, "\n * Signature: ");
422 utf_fprint(file, m->descriptor);
423 fprintf(file, "\n */\n");
425 /* create prototype */
426 fprintf(file, "JNIEXPORT ");
428 fprintf(file, " JNICALL Java_");
429 printID(m->class->name);
431 chain_addlast(ident_chain, m->name);
435 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
436 fprintf(file, "(JNIEnv *env");
438 utf_ptr = m->descriptor->text + 1;
440 if (!(m->flags & ACC_STATIC)) {
441 fprintf(file, ", struct ");
442 printID(m->class->name);
443 fprintf(file, "* this");
446 fprintf(file, ", jclass clazz");
449 if ((*utf_ptr) != ')') fprintf(file, ", ");
451 while ((*utf_ptr) != ')') {
452 utf_ptr = printtype(utf_ptr);
453 fprintf(file, " par%d", paramnum++);
454 if ((*utf_ptr)!=')') fprintf(file, ", ");
457 fprintf(file, ");\n\n");
461 /******* remove package-name in fully-qualified classname *********************/
463 void gen_header_filename(char *buffer, utf *u)
467 for (i = 0; i < utf_strlen(u); i++) {
468 if ((u->text[i] == '/') || (u->text[i] == '$')) {
469 buffer[i] = '_'; /* convert '$' and '/' to '_' */
472 buffer[i] = u->text[i];
475 buffer[utf_strlen(u)] = '\0';
479 /* create headerfile for classes and store native methods in chain ************/
481 void headerfile_generate(classinfo *c)
483 char header_filename[1024] = "";
484 char classname[1024];
485 char uclassname[1024];
490 u2 nativelyoverloaded;
492 /* store class in chain */
493 chain_addlast(nativeclass_chain, c);
495 /* open headerfile for class */
496 gen_header_filename(classname, c->name);
498 /* create chain for renaming fields */
499 ident_chain = chain_new();
501 sprintf(header_filename, "nat/%s.h", classname);
502 file = fopen(header_filename, "w");
503 if (!file) panic("Can not open file to store header information");
505 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
507 /* convert to uppercase */
508 for (i = 0; classname[i]; i++) {
509 uclassname[i] = toupper(classname[i]);
511 uclassname[i] = '\0';
513 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
515 /* create structure for direct access to objects */
516 fprintf(file, "/* Structure information for class: ");
517 utf_fprint(file, c->name);
518 fprintf(file, " */\n\n");
519 fprintf(file, "typedef struct ");
521 fprintf(file, " {\n");
529 fprintf(file, ";\n\n");
531 /* create chain for renaming overloaded methods */
532 chain_free(ident_chain);
533 ident_chain = chain_new();
535 /* create method-prototypes */
537 /* find overloaded methods */
538 for (i = 0; i < c->methodscount; i++) {
540 m = &(c->methods[i]);
542 if (!(m->flags & ACC_NATIVE)) continue;
543 if (!m->nativelyoverloaded) {
544 nativelyoverloaded=false;
545 for (i2=i+1;i2<c->methodscount; i2++) {
546 m2 = &(c->methods[i2]);
547 if (!(m2->flags & ACC_NATIVE)) continue;
548 if (m->name==m2->name) {
549 m2->nativelyoverloaded=true;
550 nativelyoverloaded=true;
553 m->nativelyoverloaded=nativelyoverloaded;
558 for (i = 0; i < c->methodscount; i++) {
560 m = &(c->methods[i]);
562 if (m->flags & ACC_NATIVE) {
563 chain_addlast(nativemethod_chain, m);
568 chain_free(ident_chain);
570 fprintf(file, "#endif\n\n");
576 /******** print classname, '$' used to seperate inner-class name ***********/
578 void print_classname(classinfo *clazz)
580 utf *u = clazz->name;
581 char *endpos = u->text + u->blength;
582 char *utf_ptr = u->text;
585 while (utf_ptr < endpos) {
586 if ((c = utf_nextu2(&utf_ptr)) == '_') {
596 /*************** create table for locating native functions ****************/
598 void printnativetableentry(methodinfo *m)
600 fprintf(file, " { \"");
601 print_classname(m->class);
602 fprintf(file, "\",\n \"");
603 utf_fprint(file, m->name);
604 fprintf(file, "\",\n \"");
605 utf_fprint(file, m->descriptor);
606 fprintf(file, "\",\n ");
608 if ((m->flags & ACC_STATIC) != 0)
609 fprintf(file, "true");
611 fprintf(file, "false");
613 fprintf(file, ",\n ");
614 fprintf(file, "(functionptr) Java_");
615 printID(m->class->name);
618 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
619 fprintf(file,"\n },\n");
623 void setVMClassField(classinfo *c)
628 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this) {
634 * These are local overrides for various environment variables in Emacs.
635 * Please do not remove this and leave it at the end of the file, where
636 * Emacs will automagically detect them.
637 * ---------------------------------------------------------------------
640 * indent-tabs-mode: t