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 1296 2004-07-10 17:02:15Z stefan $
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 use_class_as_object() {}
169 void asm_builtin_monitorenter(java_objectheader *o) {}
170 void asm_builtin_monitorexit(java_objectheader *o) {}
172 s4 asm_builtin_checkarraycast(java_objectheader *obj, vftbl_t *target) {return 0;}
174 #if defined(__MIPS__)
175 void atomic_swap() {}
176 void compare_and_swap() {}
180 #if defined(__I386__)
181 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) { return 0; }
182 void asm_builtin_newarray(s4 size, vftbl_t *arrayvftbl) {}
185 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
187 u1 *createcompilerstub(methodinfo *m) {return NULL;}
188 u1 *createnativestub(functionptr f, methodinfo *m) {return NULL;}
189 u1 *oldcreatenativestub(functionptr f, methodinfo *m) {return NULL;}
191 void removecompilerstub(u1 *stub) {}
192 void removenativestub(u1 *stub) {}
194 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
195 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
196 void thread_restartcriticalsection() {}
197 void asm_switchstackandcall() {}
198 void asm_handle_builtin_exception(classinfo *c) {}
199 void asm_getclassvalues_atomic() {}
201 #if defined(__DARWIN__)
202 int cacao_catch_Handler() {}
205 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
206 threadcritnode asm_criticalsections;
209 java_objectheader *native_new_and_init(void *p) { return NULL; }
212 /************************ global variables **********************/
214 THREADSPECIFIC java_objectheader *_exceptionptr;
216 chain *nativemethod_chain; /* chain with native methods */
217 chain *nativeclass_chain; /* chain with processed classes */
218 static chain *ident_chain; /* chain with method and field names in current class */
220 static u4 outputsize;
221 static bool dopadding;
224 static void printIDpart(int c)
226 if ((c >= 'a' && c <= 'z')
227 || (c >= 'A' && c <= 'Z')
228 || (c >= '0' && c <= '9')
236 static void printID(utf *u)
238 char *utf_ptr = u->text;
241 for (i = 0; i < utf_strlen(u); i++)
242 printIDpart(utf_nextu2(&utf_ptr));
246 static void addoutputsize (int len)
249 if (!dopadding) return;
251 newsize = ALIGN(outputsize, len);
253 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
254 outputsize = newsize;
257 static void printOverloadPart(utf *desc)
259 char *utf_ptr=desc->text;
263 while ((c=utf_nextu2(&utf_ptr))!=')') {
273 fprintf (file, "%c",(char)c);
280 while ( (c=utf_nextu2(&utf_ptr)) != ';')
286 default: panic ("invalid method descriptor");
291 static char *printtype(char *utf_ptr)
295 switch (utf_nextu2(&utf_ptr)) {
296 case 'V': fprintf (file, "void");
302 case 'Z': addoutputsize (4);
303 fprintf (file, "s4");
305 case 'J': addoutputsize (8);
306 fprintf (file, "s8");
308 case 'F': addoutputsize (4);
309 fprintf (file, "float");
311 case 'D': addoutputsize (8);
312 fprintf (file, "double");
315 addoutputsize ( sizeof(java_arrayheader*) );
316 switch (utf_nextu2(&utf_ptr)) {
317 case 'I': fprintf (file, "java_intarray*"); break;
318 case 'J': fprintf (file, "java_longarray*"); break;
319 case 'Z': fprintf (file, "java_booleanarray*"); break;
320 case 'B': fprintf (file, "java_bytearray*"); break;
321 case 'S': fprintf (file, "java_shortarray*"); break;
322 case 'C': fprintf (file, "java_chararray*"); break;
323 case 'F': fprintf (file, "java_floatarray*"); break;
324 case 'D': fprintf (file, "java_doublearray*"); break;
326 case '[': fprintf (file, "java_objectarray*");
327 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
329 while (utf_nextu2(&utf_ptr) != ';');
332 case 'L': fprintf (file, "java_objectarray*");
333 while ( utf_nextu2(&utf_ptr) != ';');
335 default: panic ("invalid type descriptor");
340 addoutputsize ( sizeof(java_objectheader*));
341 fprintf (file, "struct ");
342 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
346 default: panic ("Unknown type in field descriptor");
353 /***** determine the number of entries of a utf string in the ident chain *****/
355 static int searchidentchain_utf(utf *ident)
357 utf *u = chain_first(ident_chain); /* first element of list */
361 if (u==ident) count++; /* string found */
362 u = chain_next(ident_chain); /* next element in list */
369 /************** print structure for direct access to objects ******************/
371 static void printfields(classinfo *c)
378 addoutputsize(sizeof(java_objectheader));
379 fprintf(file, " java_objectheader header;\n");
383 printfields(c->super);
385 for (i = 0; i < c->fieldscount; i++) {
388 if (!(f->flags & ACC_STATIC)) {
390 printtype(f->descriptor->text);
392 utf_fprint(file, f->name);
394 /* rename multiple fieldnames */
395 if ((ident_count = searchidentchain_utf(f->name)))
396 fprintf(file, "%d", ident_count - 1);
397 chain_addlast(ident_chain, f->name);
399 fprintf(file, ";\n");
405 /***************** store prototype for native method in file ******************/
407 void printmethod(methodinfo *m)
413 /* search for return-type in descriptor */
414 utf_ptr = m->descriptor->text;
415 while (utf_nextu2(&utf_ptr) != ')');
418 fprintf(file, "\n/*\n * Class: ");
419 utf_fprint(file, m->class->name);
420 fprintf(file, "\n * Method: ");
421 utf_fprint(file, m->name);
422 fprintf(file, "\n * Signature: ");
423 utf_fprint(file, m->descriptor);
424 fprintf(file, "\n */\n");
426 /* create prototype */
427 fprintf(file, "JNIEXPORT ");
429 fprintf(file, " JNICALL Java_");
430 printID(m->class->name);
432 chain_addlast(ident_chain, m->name);
436 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
437 fprintf(file, "(JNIEnv *env");
439 utf_ptr = m->descriptor->text + 1;
441 if (!(m->flags & ACC_STATIC)) {
442 fprintf(file, ", struct ");
443 printID(m->class->name);
444 fprintf(file, "* this");
447 fprintf(file, ", jclass clazz");
450 if ((*utf_ptr) != ')') fprintf(file, ", ");
452 while ((*utf_ptr) != ')') {
453 utf_ptr = printtype(utf_ptr);
454 fprintf(file, " par%d", paramnum++);
455 if ((*utf_ptr)!=')') fprintf(file, ", ");
458 fprintf(file, ");\n\n");
462 /******* remove package-name in fully-qualified classname *********************/
464 void gen_header_filename(char *buffer, utf *u)
468 for (i = 0; i < utf_strlen(u); i++) {
469 if ((u->text[i] == '/') || (u->text[i] == '$')) {
470 buffer[i] = '_'; /* convert '$' and '/' to '_' */
473 buffer[i] = u->text[i];
476 buffer[utf_strlen(u)] = '\0';
480 /* create headerfile for classes and store native methods in chain ************/
482 void headerfile_generate(classinfo *c)
484 char header_filename[1024] = "";
485 char classname[1024];
486 char uclassname[1024];
491 u2 nativelyoverloaded;
493 /* store class in chain */
494 chain_addlast(nativeclass_chain, c);
496 /* open headerfile for class */
497 gen_header_filename(classname, c->name);
499 /* create chain for renaming fields */
500 ident_chain = chain_new();
502 sprintf(header_filename, "nat/%s.h", classname);
503 file = fopen(header_filename, "w");
504 if (!file) panic("Can not open file to store header information");
506 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
508 /* convert to uppercase */
509 for (i = 0; classname[i]; i++) {
510 uclassname[i] = toupper(classname[i]);
512 uclassname[i] = '\0';
514 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
516 /* create structure for direct access to objects */
517 fprintf(file, "/* Structure information for class: ");
518 utf_fprint(file, c->name);
519 fprintf(file, " */\n\n");
520 fprintf(file, "typedef struct ");
522 fprintf(file, " {\n");
530 fprintf(file, ";\n\n");
532 /* create chain for renaming overloaded methods */
533 chain_free(ident_chain);
534 ident_chain = chain_new();
536 /* create method-prototypes */
538 /* find overloaded methods */
539 for (i = 0; i < c->methodscount; i++) {
541 m = &(c->methods[i]);
543 if (!(m->flags & ACC_NATIVE)) continue;
544 if (!m->nativelyoverloaded) {
545 nativelyoverloaded=false;
546 for (i2=i+1;i2<c->methodscount; i2++) {
547 m2 = &(c->methods[i2]);
548 if (!(m2->flags & ACC_NATIVE)) continue;
549 if (m->name==m2->name) {
550 m2->nativelyoverloaded=true;
551 nativelyoverloaded=true;
554 m->nativelyoverloaded=nativelyoverloaded;
559 for (i = 0; i < c->methodscount; i++) {
561 m = &(c->methods[i]);
563 if (m->flags & ACC_NATIVE) {
564 chain_addlast(nativemethod_chain, m);
569 chain_free(ident_chain);
571 fprintf(file, "#endif\n\n");
577 /******** print classname, '$' used to seperate inner-class name ***********/
579 void print_classname(classinfo *clazz)
581 utf *u = clazz->name;
582 char *endpos = u->text + u->blength;
583 char *utf_ptr = u->text;
586 while (utf_ptr < endpos) {
587 if ((c = utf_nextu2(&utf_ptr)) == '_') {
597 /*************** create table for locating native functions ****************/
599 void printnativetableentry(methodinfo *m)
601 fprintf(file, " { \"");
602 print_classname(m->class);
603 fprintf(file, "\",\n \"");
604 utf_fprint(file, m->name);
605 fprintf(file, "\",\n \"");
606 utf_fprint(file, m->descriptor);
607 fprintf(file, "\",\n ");
609 if ((m->flags & ACC_STATIC) != 0)
610 fprintf(file, "true");
612 fprintf(file, "false");
614 fprintf(file, ",\n ");
615 fprintf(file, "(functionptr) Java_");
616 printID(m->class->name);
619 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
620 fprintf(file,"\n },\n");
624 void setVMClassField(classinfo *c)
629 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this) {
635 * These are local overrides for various environment variables in Emacs.
636 * Please do not remove this and leave it at the end of the file, where
637 * Emacs will automagically detect them.
638 * ---------------------------------------------------------------------
641 * indent-tabs-mode: t