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 1492 2004-11-12 13:26:03Z 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) { return NULL; }
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)
341 /* search for return-type in descriptor */
342 utf_ptr = m->descriptor->text;
343 while (utf_nextu2(&utf_ptr) != ')');
346 fprintf(file, "\n/*\n * Class: ");
347 utf_fprint(file, m->class->name);
348 fprintf(file, "\n * Method: ");
349 utf_fprint(file, m->name);
350 fprintf(file, "\n * Signature: ");
351 utf_fprint(file, m->descriptor);
352 fprintf(file, "\n */\n");
354 /* create prototype */
355 fprintf(file, "JNIEXPORT ");
357 fprintf(file, " JNICALL Java_");
358 printID(m->class->name);
360 chain_addlast(ident_chain, m->name);
364 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
365 fprintf(file, "(JNIEnv *env");
367 utf_ptr = m->descriptor->text + 1;
369 if (!(m->flags & ACC_STATIC)) {
370 fprintf(file, ", struct ");
371 printID(m->class->name);
372 fprintf(file, "* this");
375 fprintf(file, ", jclass clazz");
378 if ((*utf_ptr) != ')') fprintf(file, ", ");
380 while ((*utf_ptr) != ')') {
381 utf_ptr = printtype(utf_ptr);
382 fprintf(file, " par%d", paramnum++);
383 if ((*utf_ptr)!=')') fprintf(file, ", ");
386 fprintf(file, ");\n\n");
390 /******* remove package-name in fully-qualified classname *********************/
392 void gen_header_filename(char *buffer, utf *u)
396 for (i = 0; i < utf_strlen(u); i++) {
397 if ((u->text[i] == '/') || (u->text[i] == '$')) {
398 buffer[i] = '_'; /* convert '$' and '/' to '_' */
401 buffer[i] = u->text[i];
404 buffer[utf_strlen(u)] = '\0';
408 /* create headerfile for classes and store native methods in chain ************/
410 void headerfile_generate(classinfo *c)
412 char header_filename[1024] = "";
413 char classname[1024];
414 char uclassname[1024];
419 u2 nativelyoverloaded;
421 /* store class in chain */
422 chain_addlast(nativeclass_chain, c);
424 /* open headerfile for class */
425 gen_header_filename(classname, c->name);
427 /* create chain for renaming fields */
428 ident_chain = chain_new();
430 sprintf(header_filename, "nat/%s.h", classname);
431 file = fopen(header_filename, "w");
432 if (!file) panic("Can not open file to store header information");
434 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
436 /* convert to uppercase */
437 for (i = 0; classname[i]; i++) {
438 uclassname[i] = toupper(classname[i]);
440 uclassname[i] = '\0';
442 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
444 /* create structure for direct access to objects */
445 fprintf(file, "/* Structure information for class: ");
446 utf_fprint(file, c->name);
447 fprintf(file, " */\n\n");
448 fprintf(file, "typedef struct ");
450 fprintf(file, " {\n");
458 fprintf(file, ";\n\n");
460 /* create chain for renaming overloaded methods */
461 chain_free(ident_chain);
462 ident_chain = chain_new();
464 /* create method-prototypes */
466 /* find overloaded methods */
467 for (i = 0; i < c->methodscount; i++) {
469 m = &(c->methods[i]);
471 if (!(m->flags & ACC_NATIVE)) continue;
472 if (!m->nativelyoverloaded) {
473 nativelyoverloaded=false;
474 for (i2=i+1;i2<c->methodscount; i2++) {
475 m2 = &(c->methods[i2]);
476 if (!(m2->flags & ACC_NATIVE)) continue;
477 if (m->name==m2->name) {
478 m2->nativelyoverloaded=true;
479 nativelyoverloaded=true;
482 m->nativelyoverloaded=nativelyoverloaded;
487 for (i = 0; i < c->methodscount; i++) {
489 m = &(c->methods[i]);
491 if (m->flags & ACC_NATIVE) {
492 chain_addlast(nativemethod_chain, m);
497 chain_free(ident_chain);
499 fprintf(file, "#endif\n\n");
505 /******** print classname, '$' used to seperate inner-class name ***********/
507 void print_classname(classinfo *clazz)
509 utf *u = clazz->name;
510 char *endpos = u->text + u->blength;
511 char *utf_ptr = u->text;
514 while (utf_ptr < endpos) {
515 if ((c = utf_nextu2(&utf_ptr)) == '_') {
525 /*************** create table for locating native functions ****************/
527 void printnativetableentry(methodinfo *m)
529 fprintf(file, " { \"");
530 print_classname(m->class);
531 fprintf(file, "\",\n \"");
532 utf_fprint(file, m->name);
533 fprintf(file, "\",\n \"");
534 utf_fprint(file, m->descriptor);
535 fprintf(file, "\",\n ");
537 if ((m->flags & ACC_STATIC) != 0)
538 fprintf(file, "true");
540 fprintf(file, "false");
542 fprintf(file, ",\n ");
543 fprintf(file, "(functionptr) Java_");
544 printID(m->class->name);
547 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
548 fprintf(file,"\n },\n");
552 void setVMClassField(classinfo *c)
557 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this) {
563 * These are local overrides for various environment variables in Emacs.
564 * Please do not remove this and leave it at the end of the file, where
565 * Emacs will automagically detect them.
566 * ---------------------------------------------------------------------
569 * indent-tabs-mode: t