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 1357 2004-07-28 10:22:05Z twisti $
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 void atomic_swap() {}
101 void compare_and_swap() {}
105 #if defined(__I386__)
106 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) { return 0; }
107 void asm_builtin_newarray(s4 size, vftbl_t *arrayvftbl) {}
110 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
112 u1 *createcompilerstub(methodinfo *m) {return NULL;}
113 u1 *createnativestub(functionptr f, methodinfo *m) {return NULL;}
114 u1 *oldcreatenativestub(functionptr f, methodinfo *m) {return NULL;}
116 void removecompilerstub(u1 *stub) {}
117 void removenativestub(u1 *stub) {}
119 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
120 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
121 void thread_restartcriticalsection() {}
122 void asm_switchstackandcall() {}
123 void asm_handle_builtin_exception(classinfo *c) {}
124 void asm_getclassvalues_atomic() {}
126 #if defined(__DARWIN__)
127 int cacao_catch_Handler() {}
130 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
131 threadcritnode asm_criticalsections;
135 /************************ global variables **********************/
137 THREADSPECIFIC java_objectheader *_exceptionptr;
139 chain *nativemethod_chain; /* chain with native methods */
140 chain *nativeclass_chain; /* chain with processed classes */
141 static chain *ident_chain; /* chain with method and field names in current class */
143 static u4 outputsize;
144 static bool dopadding;
147 static void printIDpart(int c)
149 if ((c >= 'a' && c <= 'z')
150 || (c >= 'A' && c <= 'Z')
151 || (c >= '0' && c <= '9')
159 static void printID(utf *u)
161 char *utf_ptr = u->text;
164 for (i = 0; i < utf_strlen(u); i++)
165 printIDpart(utf_nextu2(&utf_ptr));
169 static void addoutputsize (int len)
172 if (!dopadding) return;
174 newsize = ALIGN(outputsize, len);
176 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
177 outputsize = newsize;
180 static void printOverloadPart(utf *desc)
182 char *utf_ptr=desc->text;
186 while ((c=utf_nextu2(&utf_ptr))!=')') {
196 fprintf (file, "%c",(char)c);
203 while ( (c=utf_nextu2(&utf_ptr)) != ';')
209 default: panic ("invalid method descriptor");
214 static char *printtype(char *utf_ptr)
218 switch (utf_nextu2(&utf_ptr)) {
219 case 'V': fprintf (file, "void");
225 case 'Z': addoutputsize (4);
226 fprintf (file, "s4");
228 case 'J': addoutputsize (8);
229 fprintf (file, "s8");
231 case 'F': addoutputsize (4);
232 fprintf (file, "float");
234 case 'D': addoutputsize (8);
235 fprintf (file, "double");
238 addoutputsize ( sizeof(java_arrayheader*) );
239 switch (utf_nextu2(&utf_ptr)) {
240 case 'I': fprintf (file, "java_intarray*"); break;
241 case 'J': fprintf (file, "java_longarray*"); break;
242 case 'Z': fprintf (file, "java_booleanarray*"); break;
243 case 'B': fprintf (file, "java_bytearray*"); break;
244 case 'S': fprintf (file, "java_shortarray*"); break;
245 case 'C': fprintf (file, "java_chararray*"); break;
246 case 'F': fprintf (file, "java_floatarray*"); break;
247 case 'D': fprintf (file, "java_doublearray*"); break;
249 case '[': fprintf (file, "java_objectarray*");
250 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
252 while (utf_nextu2(&utf_ptr) != ';');
255 case 'L': fprintf (file, "java_objectarray*");
256 while ( utf_nextu2(&utf_ptr) != ';');
258 default: panic ("invalid type descriptor");
263 addoutputsize ( sizeof(java_objectheader*));
264 fprintf (file, "struct ");
265 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
269 default: panic ("Unknown type in field descriptor");
276 /***** determine the number of entries of a utf string in the ident chain *****/
278 static int searchidentchain_utf(utf *ident)
280 utf *u = chain_first(ident_chain); /* first element of list */
284 if (u==ident) count++; /* string found */
285 u = chain_next(ident_chain); /* next element in list */
292 /************** print structure for direct access to objects ******************/
294 static void printfields(classinfo *c)
301 addoutputsize(sizeof(java_objectheader));
302 fprintf(file, " java_objectheader header;\n");
306 printfields(c->super);
308 for (i = 0; i < c->fieldscount; i++) {
311 if (!(f->flags & ACC_STATIC)) {
313 printtype(f->descriptor->text);
315 utf_fprint(file, f->name);
317 /* rename multiple fieldnames */
318 if ((ident_count = searchidentchain_utf(f->name)))
319 fprintf(file, "%d", ident_count - 1);
320 chain_addlast(ident_chain, f->name);
322 fprintf(file, ";\n");
328 /***************** store prototype for native method in file ******************/
330 void printmethod(methodinfo *m)
336 /* search for return-type in descriptor */
337 utf_ptr = m->descriptor->text;
338 while (utf_nextu2(&utf_ptr) != ')');
341 fprintf(file, "\n/*\n * Class: ");
342 utf_fprint(file, m->class->name);
343 fprintf(file, "\n * Method: ");
344 utf_fprint(file, m->name);
345 fprintf(file, "\n * Signature: ");
346 utf_fprint(file, m->descriptor);
347 fprintf(file, "\n */\n");
349 /* create prototype */
350 fprintf(file, "JNIEXPORT ");
352 fprintf(file, " JNICALL Java_");
353 printID(m->class->name);
355 chain_addlast(ident_chain, m->name);
359 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
360 fprintf(file, "(JNIEnv *env");
362 utf_ptr = m->descriptor->text + 1;
364 if (!(m->flags & ACC_STATIC)) {
365 fprintf(file, ", struct ");
366 printID(m->class->name);
367 fprintf(file, "* this");
370 fprintf(file, ", jclass clazz");
373 if ((*utf_ptr) != ')') fprintf(file, ", ");
375 while ((*utf_ptr) != ')') {
376 utf_ptr = printtype(utf_ptr);
377 fprintf(file, " par%d", paramnum++);
378 if ((*utf_ptr)!=')') fprintf(file, ", ");
381 fprintf(file, ");\n\n");
385 /******* remove package-name in fully-qualified classname *********************/
387 void gen_header_filename(char *buffer, utf *u)
391 for (i = 0; i < utf_strlen(u); i++) {
392 if ((u->text[i] == '/') || (u->text[i] == '$')) {
393 buffer[i] = '_'; /* convert '$' and '/' to '_' */
396 buffer[i] = u->text[i];
399 buffer[utf_strlen(u)] = '\0';
403 /* create headerfile for classes and store native methods in chain ************/
405 void headerfile_generate(classinfo *c)
407 char header_filename[1024] = "";
408 char classname[1024];
409 char uclassname[1024];
414 u2 nativelyoverloaded;
416 /* store class in chain */
417 chain_addlast(nativeclass_chain, c);
419 /* open headerfile for class */
420 gen_header_filename(classname, c->name);
422 /* create chain for renaming fields */
423 ident_chain = chain_new();
425 sprintf(header_filename, "nat/%s.h", classname);
426 file = fopen(header_filename, "w");
427 if (!file) panic("Can not open file to store header information");
429 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
431 /* convert to uppercase */
432 for (i = 0; classname[i]; i++) {
433 uclassname[i] = toupper(classname[i]);
435 uclassname[i] = '\0';
437 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
439 /* create structure for direct access to objects */
440 fprintf(file, "/* Structure information for class: ");
441 utf_fprint(file, c->name);
442 fprintf(file, " */\n\n");
443 fprintf(file, "typedef struct ");
445 fprintf(file, " {\n");
453 fprintf(file, ";\n\n");
455 /* create chain for renaming overloaded methods */
456 chain_free(ident_chain);
457 ident_chain = chain_new();
459 /* create method-prototypes */
461 /* find overloaded methods */
462 for (i = 0; i < c->methodscount; i++) {
464 m = &(c->methods[i]);
466 if (!(m->flags & ACC_NATIVE)) continue;
467 if (!m->nativelyoverloaded) {
468 nativelyoverloaded=false;
469 for (i2=i+1;i2<c->methodscount; i2++) {
470 m2 = &(c->methods[i2]);
471 if (!(m2->flags & ACC_NATIVE)) continue;
472 if (m->name==m2->name) {
473 m2->nativelyoverloaded=true;
474 nativelyoverloaded=true;
477 m->nativelyoverloaded=nativelyoverloaded;
482 for (i = 0; i < c->methodscount; i++) {
484 m = &(c->methods[i]);
486 if (m->flags & ACC_NATIVE) {
487 chain_addlast(nativemethod_chain, m);
492 chain_free(ident_chain);
494 fprintf(file, "#endif\n\n");
500 /******** print classname, '$' used to seperate inner-class name ***********/
502 void print_classname(classinfo *clazz)
504 utf *u = clazz->name;
505 char *endpos = u->text + u->blength;
506 char *utf_ptr = u->text;
509 while (utf_ptr < endpos) {
510 if ((c = utf_nextu2(&utf_ptr)) == '_') {
520 /*************** create table for locating native functions ****************/
522 void printnativetableentry(methodinfo *m)
524 fprintf(file, " { \"");
525 print_classname(m->class);
526 fprintf(file, "\",\n \"");
527 utf_fprint(file, m->name);
528 fprintf(file, "\",\n \"");
529 utf_fprint(file, m->descriptor);
530 fprintf(file, "\",\n ");
532 if ((m->flags & ACC_STATIC) != 0)
533 fprintf(file, "true");
535 fprintf(file, "false");
537 fprintf(file, ",\n ");
538 fprintf(file, "(functionptr) Java_");
539 printID(m->class->name);
542 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
543 fprintf(file,"\n },\n");
547 void setVMClassField(classinfo *c)
552 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this) {
558 * These are local overrides for various environment variables in Emacs.
559 * Please do not remove this and leave it at the end of the file, where
560 * Emacs will automagically detect them.
561 * ---------------------------------------------------------------------
564 * indent-tabs-mode: t