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 1570 2004-11-23 16:04:33Z twisti $
50 #include "toolbox/chain.h"
51 #include "toolbox/memory.h"
52 #include "toolbox/logging.h"
53 #include "nat/java_lang_String.h"
54 #include "nat/java_lang_Throwable.h"
57 /******* replace some external functions *********/
59 functionptr native_findfunction(utf *cname, utf *mname, utf *desc, bool isstatic)
62 java_objectheader *javastring_new(utf *text) { return NULL; }
63 java_objectheader *javastring_new_char(char *text) { return NULL; }
65 char *javastring_tochar(java_objectheader *so) { return NULL; }
66 utf *javastring_toutf(java_lang_String *string, bool isclassname)
70 java_objectheader *native_new_and_init(classinfo *c) { return NULL; }
71 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s) { return NULL; }
72 java_objectheader *native_new_and_init_int(classinfo *c, s4 i) { return NULL; }
73 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t) { return NULL; }
75 java_objectheader *literalstring_new(utf *u) { return NULL; }
78 void literalstring_free(java_objectheader *o) {}
79 void stringtable_update() { }
80 void synchronize_caches() { }
81 void asm_call_jit_compiler() { }
82 void asm_calljavafunction() { }
83 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
85 s4 asm_builtin_idiv(s4 a, s4 b) {return 0;}
86 s4 asm_builtin_irem(s4 a, s4 b) {return 0;}
87 s8 asm_builtin_ldiv(s8 a, s8 b) {return 0;}
88 s8 asm_builtin_lrem(s8 a, s8 b) {return 0;}
90 s4 asm_builtin_f2i(float a) { return 0; }
91 s8 asm_builtin_f2l(float a) { return 0; }
92 s4 asm_builtin_d2i(double a) { return 0; }
93 s8 asm_builtin_d2l(double a) { return 0; }
95 void use_class_as_object() {}
96 void asm_builtin_monitorenter(java_objectheader *o) {}
97 void *asm_builtin_monitorexit(java_objectheader *o) { return NULL; }
99 s4 asm_builtin_checkarraycast(java_objectheader *obj, vftbl_t *target) {return 0;}
101 #if defined(__MIPS__)
102 long compare_and_swap(long *p, long oldval, long newval)
113 #if defined(__I386__)
114 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) { return 0; }
115 void asm_builtin_newarray(s4 size, vftbl_t *arrayvftbl) {}
118 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
120 u1 *createcompilerstub(methodinfo *m) {return NULL;}
121 u1 *createnativestub(functionptr f, methodinfo *m) {return NULL;}
122 u1 *oldcreatenativestub(functionptr f, methodinfo *m) {return NULL;}
124 void removecompilerstub(u1 *stub) {}
125 void removenativestub(u1 *stub) {}
127 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
128 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
129 void thread_restartcriticalsection(ucontext_t *uc) {}
130 void asm_switchstackandcall() {}
131 void asm_handle_builtin_exception(classinfo *c) {}
132 void asm_getclassvalues_atomic() {}
134 #if defined(__DARWIN__)
135 int cacao_catch_Handler() {}
138 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
139 threadcritnode asm_criticalsections;
143 /************************ global variables **********************/
145 THREADSPECIFIC java_objectheader *_exceptionptr;
147 chain *nativemethod_chain; /* chain with native methods */
148 chain *nativeclass_chain; /* chain with processed classes */
149 static chain *ident_chain; /* chain with method and field names in current class */
151 static u4 outputsize;
152 static bool dopadding;
155 static void printIDpart(int c)
157 if ((c >= 'a' && c <= 'z')
158 || (c >= 'A' && c <= 'Z')
159 || (c >= '0' && c <= '9')
167 static void printID(utf *u)
169 char *utf_ptr = u->text;
172 for (i = 0; i < utf_strlen(u); i++)
173 printIDpart(utf_nextu2(&utf_ptr));
177 static void addoutputsize (int len)
180 if (!dopadding) return;
182 newsize = ALIGN(outputsize, len);
184 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
185 outputsize = newsize;
188 static void printOverloadPart(utf *desc)
190 char *utf_ptr=desc->text;
194 while ((c=utf_nextu2(&utf_ptr))!=')') {
204 fprintf (file, "%c",(char)c);
211 while ( (c=utf_nextu2(&utf_ptr)) != ';')
217 default: panic ("invalid method descriptor");
222 static char *printtype(char *utf_ptr)
226 switch (utf_nextu2(&utf_ptr)) {
227 case 'V': fprintf (file, "void");
233 case 'Z': addoutputsize (4);
234 fprintf (file, "s4");
236 case 'J': addoutputsize (8);
237 fprintf (file, "s8");
239 case 'F': addoutputsize (4);
240 fprintf (file, "float");
242 case 'D': addoutputsize (8);
243 fprintf (file, "double");
246 addoutputsize ( sizeof(java_arrayheader*) );
247 switch (utf_nextu2(&utf_ptr)) {
248 case 'I': fprintf (file, "java_intarray*"); break;
249 case 'J': fprintf (file, "java_longarray*"); break;
250 case 'Z': fprintf (file, "java_booleanarray*"); break;
251 case 'B': fprintf (file, "java_bytearray*"); break;
252 case 'S': fprintf (file, "java_shortarray*"); break;
253 case 'C': fprintf (file, "java_chararray*"); break;
254 case 'F': fprintf (file, "java_floatarray*"); break;
255 case 'D': fprintf (file, "java_doublearray*"); break;
257 case '[': fprintf (file, "java_objectarray*");
258 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
260 while (utf_nextu2(&utf_ptr) != ';');
263 case 'L': fprintf (file, "java_objectarray*");
264 while ( utf_nextu2(&utf_ptr) != ';');
266 default: panic ("invalid type descriptor");
271 addoutputsize ( sizeof(java_objectheader*));
272 fprintf (file, "struct ");
273 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
277 default: panic ("Unknown type in field descriptor");
284 /***** determine the number of entries of a utf string in the ident chain *****/
286 static int searchidentchain_utf(utf *ident)
288 utf *u = chain_first(ident_chain); /* first element of list */
292 if (u==ident) count++; /* string found */
293 u = chain_next(ident_chain); /* next element in list */
300 /************** print structure for direct access to objects ******************/
302 static void printfields(classinfo *c)
309 addoutputsize(sizeof(java_objectheader));
310 fprintf(file, " java_objectheader header;\n");
314 printfields(c->super);
316 for (i = 0; i < c->fieldscount; i++) {
319 if (!(f->flags & ACC_STATIC)) {
321 printtype(f->descriptor->text);
323 utf_fprint(file, f->name);
325 /* rename multiple fieldnames */
326 if ((ident_count = searchidentchain_utf(f->name)))
327 fprintf(file, "%d", ident_count - 1);
328 chain_addlast(ident_chain, f->name);
330 fprintf(file, ";\n");
336 /***************** store prototype for native method in file ******************/
338 void printmethod(methodinfo *m)
343 /* search for return-type in descriptor */
344 utf_ptr = m->descriptor->text;
345 while (utf_nextu2(&utf_ptr) != ')');
348 fprintf(file, "\n/*\n * Class: ");
349 utf_fprint(file, m->class->name);
350 fprintf(file, "\n * Method: ");
351 utf_fprint(file, m->name);
352 fprintf(file, "\n * Signature: ");
353 utf_fprint(file, m->descriptor);
354 fprintf(file, "\n */\n");
356 /* create prototype */
357 fprintf(file, "JNIEXPORT ");
359 fprintf(file, " JNICALL Java_");
360 printID(m->class->name);
362 chain_addlast(ident_chain, m->name);
366 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
367 fprintf(file, "(JNIEnv *env");
369 utf_ptr = m->descriptor->text + 1;
371 if (!(m->flags & ACC_STATIC)) {
372 fprintf(file, ", struct ");
373 printID(m->class->name);
374 fprintf(file, "* this");
377 fprintf(file, ", jclass clazz");
380 if ((*utf_ptr) != ')') fprintf(file, ", ");
382 while ((*utf_ptr) != ')') {
383 utf_ptr = printtype(utf_ptr);
384 fprintf(file, " par%d", paramnum++);
385 if ((*utf_ptr)!=')') fprintf(file, ", ");
388 fprintf(file, ");\n\n");
392 /******* remove package-name in fully-qualified classname *********************/
394 void gen_header_filename(char *buffer, utf *u)
398 for (i = 0; i < utf_strlen(u); i++) {
399 if ((u->text[i] == '/') || (u->text[i] == '$')) {
400 buffer[i] = '_'; /* convert '$' and '/' to '_' */
403 buffer[i] = u->text[i];
406 buffer[utf_strlen(u)] = '\0';
410 /* create headerfile for classes and store native methods in chain ************/
412 void headerfile_generate(classinfo *c, char *opt_directory)
414 char header_filename[1024] = "";
415 char classname[1024];
416 char uclassname[1024];
421 u2 nativelyoverloaded;
423 /* store class in chain */
424 chain_addlast(nativeclass_chain, c);
426 /* open headerfile for class */
427 gen_header_filename(classname, c->name);
429 /* create chain for renaming fields */
430 ident_chain = chain_new();
433 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
436 sprintf(header_filename, "%s.h", classname);
439 file = fopen(header_filename, "w");
441 panic("Can not open file to store header information");
443 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
445 /* convert to uppercase */
446 for (i = 0; classname[i]; i++) {
447 uclassname[i] = toupper(classname[i]);
449 uclassname[i] = '\0';
451 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
453 /* create structure for direct access to objects */
454 fprintf(file, "/* Structure information for class: ");
455 utf_fprint(file, c->name);
456 fprintf(file, " */\n\n");
457 fprintf(file, "typedef struct ");
459 fprintf(file, " {\n");
467 fprintf(file, ";\n\n");
469 /* create chain for renaming overloaded methods */
470 chain_free(ident_chain);
471 ident_chain = chain_new();
473 /* create method-prototypes */
475 /* find overloaded methods */
476 for (i = 0; i < c->methodscount; i++) {
478 m = &(c->methods[i]);
480 if (!(m->flags & ACC_NATIVE)) continue;
481 if (!m->nativelyoverloaded) {
482 nativelyoverloaded=false;
483 for (i2=i+1;i2<c->methodscount; i2++) {
484 m2 = &(c->methods[i2]);
485 if (!(m2->flags & ACC_NATIVE)) continue;
486 if (m->name==m2->name) {
487 m2->nativelyoverloaded=true;
488 nativelyoverloaded=true;
491 m->nativelyoverloaded=nativelyoverloaded;
496 for (i = 0; i < c->methodscount; i++) {
498 m = &(c->methods[i]);
500 if (m->flags & ACC_NATIVE) {
501 chain_addlast(nativemethod_chain, m);
506 chain_free(ident_chain);
508 fprintf(file, "#endif\n\n");
514 /******** print classname, '$' used to seperate inner-class name ***********/
516 void print_classname(classinfo *clazz)
518 utf *u = clazz->name;
519 char *endpos = u->text + u->blength;
520 char *utf_ptr = u->text;
523 while (utf_ptr < endpos) {
524 if ((c = utf_nextu2(&utf_ptr)) == '_') {
534 /*************** create table for locating native functions ****************/
536 void printnativetableentry(methodinfo *m)
538 fprintf(file, " { \"");
539 print_classname(m->class);
540 fprintf(file, "\",\n \"");
541 utf_fprint(file, m->name);
542 fprintf(file, "\",\n \"");
543 utf_fprint(file, m->descriptor);
544 fprintf(file, "\",\n ");
546 if ((m->flags & ACC_STATIC) != 0)
547 fprintf(file, "true");
549 fprintf(file, "false");
551 fprintf(file, ",\n ");
552 fprintf(file, "(functionptr) Java_");
553 printID(m->class->name);
556 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
557 fprintf(file,"\n },\n");
561 void setVMClassField(classinfo *c)
566 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this) {
572 * These are local overrides for various environment variables in Emacs.
573 * Please do not remove this and leave it at the end of the file, where
574 * Emacs will automagically detect them.
575 * ---------------------------------------------------------------------
578 * indent-tabs-mode: t