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 1368 2004-08-01 21:50:08Z stefan $
48 #include "toolbox/chain.h"
49 #include "toolbox/memory.h"
50 #include "toolbox/logging.h"
51 #include "nat/java_lang_String.h"
52 #include "nat/java_lang_Throwable.h"
55 /******* replace some external functions *********/
57 functionptr native_findfunction(utf *cname, utf *mname, utf *desc, bool isstatic)
60 java_objectheader *javastring_new(utf *text) { return NULL; }
61 java_objectheader *javastring_new_char(char *text) { return NULL; }
63 char *javastring_tochar(java_objectheader *so) { return NULL; }
64 utf *javastring_toutf(java_lang_String *string, bool isclassname)
68 java_objectheader *native_new_and_init(classinfo *c) { return NULL; }
69 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s) { return NULL; }
70 java_objectheader *native_new_and_init_int(classinfo *c, s4 i) { return NULL; }
71 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t) { return NULL; }
73 java_objectheader *literalstring_new(utf *u) { return NULL; }
76 void literalstring_free(java_objectheader *o) {}
77 void stringtable_update() { }
78 void synchronize_caches() { }
79 void asm_call_jit_compiler() { }
80 void asm_calljavafunction() { }
81 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
83 s4 asm_builtin_idiv(s4 a, s4 b) {return 0;}
84 s4 asm_builtin_irem(s4 a, s4 b) {return 0;}
85 s8 asm_builtin_ldiv(s8 a, s8 b) {return 0;}
86 s8 asm_builtin_lrem(s8 a, s8 b) {return 0;}
88 s4 asm_builtin_f2i(float a) { return 0; }
89 s8 asm_builtin_f2l(float a) { return 0; }
90 s4 asm_builtin_d2i(double a) { return 0; }
91 s8 asm_builtin_d2l(double a) { return 0; }
93 void use_class_as_object() {}
94 void asm_builtin_monitorenter(java_objectheader *o) {}
95 void *asm_builtin_monitorexit(java_objectheader *o) {}
97 s4 asm_builtin_checkarraycast(java_objectheader *obj, vftbl_t *target) {return 0;}
100 long compare_and_swap(long *p, long oldval, long newval)
111 #if defined(__I386__)
112 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) { return 0; }
113 void asm_builtin_newarray(s4 size, vftbl_t *arrayvftbl) {}
116 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
118 u1 *createcompilerstub(methodinfo *m) {return NULL;}
119 u1 *createnativestub(functionptr f, methodinfo *m) {return NULL;}
120 u1 *oldcreatenativestub(functionptr f, methodinfo *m) {return NULL;}
122 void removecompilerstub(u1 *stub) {}
123 void removenativestub(u1 *stub) {}
125 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
126 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
127 void thread_restartcriticalsection() {}
128 void asm_switchstackandcall() {}
129 void asm_handle_builtin_exception(classinfo *c) {}
130 void asm_getclassvalues_atomic() {}
132 #if defined(__DARWIN__)
133 int cacao_catch_Handler() {}
136 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
137 threadcritnode asm_criticalsections;
141 /************************ global variables **********************/
143 THREADSPECIFIC java_objectheader *_exceptionptr;
145 chain *nativemethod_chain; /* chain with native methods */
146 chain *nativeclass_chain; /* chain with processed classes */
147 static chain *ident_chain; /* chain with method and field names in current class */
149 static u4 outputsize;
150 static bool dopadding;
153 static void printIDpart(int c)
155 if ((c >= 'a' && c <= 'z')
156 || (c >= 'A' && c <= 'Z')
157 || (c >= '0' && c <= '9')
165 static void printID(utf *u)
167 char *utf_ptr = u->text;
170 for (i = 0; i < utf_strlen(u); i++)
171 printIDpart(utf_nextu2(&utf_ptr));
175 static void addoutputsize (int len)
178 if (!dopadding) return;
180 newsize = ALIGN(outputsize, len);
182 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
183 outputsize = newsize;
186 static void printOverloadPart(utf *desc)
188 char *utf_ptr=desc->text;
192 while ((c=utf_nextu2(&utf_ptr))!=')') {
202 fprintf (file, "%c",(char)c);
209 while ( (c=utf_nextu2(&utf_ptr)) != ';')
215 default: panic ("invalid method descriptor");
220 static char *printtype(char *utf_ptr)
224 switch (utf_nextu2(&utf_ptr)) {
225 case 'V': fprintf (file, "void");
231 case 'Z': addoutputsize (4);
232 fprintf (file, "s4");
234 case 'J': addoutputsize (8);
235 fprintf (file, "s8");
237 case 'F': addoutputsize (4);
238 fprintf (file, "float");
240 case 'D': addoutputsize (8);
241 fprintf (file, "double");
244 addoutputsize ( sizeof(java_arrayheader*) );
245 switch (utf_nextu2(&utf_ptr)) {
246 case 'I': fprintf (file, "java_intarray*"); break;
247 case 'J': fprintf (file, "java_longarray*"); break;
248 case 'Z': fprintf (file, "java_booleanarray*"); break;
249 case 'B': fprintf (file, "java_bytearray*"); break;
250 case 'S': fprintf (file, "java_shortarray*"); break;
251 case 'C': fprintf (file, "java_chararray*"); break;
252 case 'F': fprintf (file, "java_floatarray*"); break;
253 case 'D': fprintf (file, "java_doublearray*"); break;
255 case '[': fprintf (file, "java_objectarray*");
256 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
258 while (utf_nextu2(&utf_ptr) != ';');
261 case 'L': fprintf (file, "java_objectarray*");
262 while ( utf_nextu2(&utf_ptr) != ';');
264 default: panic ("invalid type descriptor");
269 addoutputsize ( sizeof(java_objectheader*));
270 fprintf (file, "struct ");
271 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
275 default: panic ("Unknown type in field descriptor");
282 /***** determine the number of entries of a utf string in the ident chain *****/
284 static int searchidentchain_utf(utf *ident)
286 utf *u = chain_first(ident_chain); /* first element of list */
290 if (u==ident) count++; /* string found */
291 u = chain_next(ident_chain); /* next element in list */
298 /************** print structure for direct access to objects ******************/
300 static void printfields(classinfo *c)
307 addoutputsize(sizeof(java_objectheader));
308 fprintf(file, " java_objectheader header;\n");
312 printfields(c->super);
314 for (i = 0; i < c->fieldscount; i++) {
317 if (!(f->flags & ACC_STATIC)) {
319 printtype(f->descriptor->text);
321 utf_fprint(file, f->name);
323 /* rename multiple fieldnames */
324 if ((ident_count = searchidentchain_utf(f->name)))
325 fprintf(file, "%d", ident_count - 1);
326 chain_addlast(ident_chain, f->name);
328 fprintf(file, ";\n");
334 /***************** store prototype for native method in file ******************/
336 void printmethod(methodinfo *m)
342 /* search for return-type in descriptor */
343 utf_ptr = m->descriptor->text;
344 while (utf_nextu2(&utf_ptr) != ')');
347 fprintf(file, "\n/*\n * Class: ");
348 utf_fprint(file, m->class->name);
349 fprintf(file, "\n * Method: ");
350 utf_fprint(file, m->name);
351 fprintf(file, "\n * Signature: ");
352 utf_fprint(file, m->descriptor);
353 fprintf(file, "\n */\n");
355 /* create prototype */
356 fprintf(file, "JNIEXPORT ");
358 fprintf(file, " JNICALL Java_");
359 printID(m->class->name);
361 chain_addlast(ident_chain, m->name);
365 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
366 fprintf(file, "(JNIEnv *env");
368 utf_ptr = m->descriptor->text + 1;
370 if (!(m->flags & ACC_STATIC)) {
371 fprintf(file, ", struct ");
372 printID(m->class->name);
373 fprintf(file, "* this");
376 fprintf(file, ", jclass clazz");
379 if ((*utf_ptr) != ')') fprintf(file, ", ");
381 while ((*utf_ptr) != ')') {
382 utf_ptr = printtype(utf_ptr);
383 fprintf(file, " par%d", paramnum++);
384 if ((*utf_ptr)!=')') fprintf(file, ", ");
387 fprintf(file, ");\n\n");
391 /******* remove package-name in fully-qualified classname *********************/
393 void gen_header_filename(char *buffer, utf *u)
397 for (i = 0; i < utf_strlen(u); i++) {
398 if ((u->text[i] == '/') || (u->text[i] == '$')) {
399 buffer[i] = '_'; /* convert '$' and '/' to '_' */
402 buffer[i] = u->text[i];
405 buffer[utf_strlen(u)] = '\0';
409 /* create headerfile for classes and store native methods in chain ************/
411 void headerfile_generate(classinfo *c)
413 char header_filename[1024] = "";
414 char classname[1024];
415 char uclassname[1024];
420 u2 nativelyoverloaded;
422 /* store class in chain */
423 chain_addlast(nativeclass_chain, c);
425 /* open headerfile for class */
426 gen_header_filename(classname, c->name);
428 /* create chain for renaming fields */
429 ident_chain = chain_new();
431 sprintf(header_filename, "nat/%s.h", classname);
432 file = fopen(header_filename, "w");
433 if (!file) panic("Can not open file to store header information");
435 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
437 /* convert to uppercase */
438 for (i = 0; classname[i]; i++) {
439 uclassname[i] = toupper(classname[i]);
441 uclassname[i] = '\0';
443 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
445 /* create structure for direct access to objects */
446 fprintf(file, "/* Structure information for class: ");
447 utf_fprint(file, c->name);
448 fprintf(file, " */\n\n");
449 fprintf(file, "typedef struct ");
451 fprintf(file, " {\n");
459 fprintf(file, ";\n\n");
461 /* create chain for renaming overloaded methods */
462 chain_free(ident_chain);
463 ident_chain = chain_new();
465 /* create method-prototypes */
467 /* find overloaded methods */
468 for (i = 0; i < c->methodscount; i++) {
470 m = &(c->methods[i]);
472 if (!(m->flags & ACC_NATIVE)) continue;
473 if (!m->nativelyoverloaded) {
474 nativelyoverloaded=false;
475 for (i2=i+1;i2<c->methodscount; i2++) {
476 m2 = &(c->methods[i2]);
477 if (!(m2->flags & ACC_NATIVE)) continue;
478 if (m->name==m2->name) {
479 m2->nativelyoverloaded=true;
480 nativelyoverloaded=true;
483 m->nativelyoverloaded=nativelyoverloaded;
488 for (i = 0; i < c->methodscount; i++) {
490 m = &(c->methods[i]);
492 if (m->flags & ACC_NATIVE) {
493 chain_addlast(nativemethod_chain, m);
498 chain_free(ident_chain);
500 fprintf(file, "#endif\n\n");
506 /******** print classname, '$' used to seperate inner-class name ***********/
508 void print_classname(classinfo *clazz)
510 utf *u = clazz->name;
511 char *endpos = u->text + u->blength;
512 char *utf_ptr = u->text;
515 while (utf_ptr < endpos) {
516 if ((c = utf_nextu2(&utf_ptr)) == '_') {
526 /*************** create table for locating native functions ****************/
528 void printnativetableentry(methodinfo *m)
530 fprintf(file, " { \"");
531 print_classname(m->class);
532 fprintf(file, "\",\n \"");
533 utf_fprint(file, m->name);
534 fprintf(file, "\",\n \"");
535 utf_fprint(file, m->descriptor);
536 fprintf(file, "\",\n ");
538 if ((m->flags & ACC_STATIC) != 0)
539 fprintf(file, "true");
541 fprintf(file, "false");
543 fprintf(file, ",\n ");
544 fprintf(file, "(functionptr) Java_");
545 printID(m->class->name);
548 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
549 fprintf(file,"\n },\n");
553 void setVMClassField(classinfo *c)
558 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this) {
564 * These are local overrides for various environment variables in Emacs.
565 * Please do not remove this and leave it at the end of the file, where
566 * Emacs will automagically detect them.
567 * ---------------------------------------------------------------------
570 * indent-tabs-mode: t