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 1327 2004-07-21 14:05:43Z twisti $
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) { return NULL; }
60 java_objectheader *javastring_new_char(char *text) { return NULL; }
62 char *javastring_tochar(java_objectheader *so) { return NULL; }
63 utf *javastring_toutf(java_lang_String *string, bool isclassname)
67 java_objectheader *native_new_and_init(classinfo *c) { return NULL; }
68 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s) { return NULL; }
69 java_objectheader *native_new_and_init_int(classinfo *c, s4 i) { return NULL; }
70 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t) { return NULL; }
72 java_objectheader *literalstring_new(utf *u) { return NULL; }
75 void literalstring_free(java_objectheader *o) {}
76 void stringtable_update() { }
77 void synchronize_caches() { }
78 void asm_call_jit_compiler() { }
79 void asm_calljavafunction() { }
80 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
82 s4 asm_builtin_idiv(s4 a, s4 b) {return 0;}
83 s4 asm_builtin_irem(s4 a, s4 b) {return 0;}
84 s8 asm_builtin_ldiv(s8 a, s8 b) {return 0;}
85 s8 asm_builtin_lrem(s8 a, s8 b) {return 0;}
87 s4 asm_builtin_f2i(float a) { return 0; }
88 s8 asm_builtin_f2l(float a) { return 0; }
89 s4 asm_builtin_d2i(double a) { return 0; }
90 s8 asm_builtin_d2l(double a) { return 0; }
92 void use_class_as_object() {}
93 void asm_builtin_monitorenter(java_objectheader *o) {}
94 void asm_builtin_monitorexit(java_objectheader *o) {}
96 s4 asm_builtin_checkarraycast(java_objectheader *obj, vftbl_t *target) {return 0;}
100 void compare_and_swap() {}
104 #if defined(__I386__)
105 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) { return 0; }
106 void asm_builtin_newarray(s4 size, vftbl_t *arrayvftbl) {}
109 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
111 u1 *createcompilerstub(methodinfo *m) {return NULL;}
112 u1 *createnativestub(functionptr f, methodinfo *m) {return NULL;}
113 u1 *oldcreatenativestub(functionptr f, methodinfo *m) {return NULL;}
115 void removecompilerstub(u1 *stub) {}
116 void removenativestub(u1 *stub) {}
118 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
119 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
120 void thread_restartcriticalsection() {}
121 void asm_switchstackandcall() {}
122 void asm_handle_builtin_exception(classinfo *c) {}
123 void asm_getclassvalues_atomic() {}
125 #if defined(__DARWIN__)
126 int cacao_catch_Handler() {}
129 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
130 threadcritnode asm_criticalsections;
134 /************************ global variables **********************/
136 THREADSPECIFIC java_objectheader *_exceptionptr;
138 chain *nativemethod_chain; /* chain with native methods */
139 chain *nativeclass_chain; /* chain with processed classes */
140 static chain *ident_chain; /* chain with method and field names in current class */
142 static u4 outputsize;
143 static bool dopadding;
146 static void printIDpart(int c)
148 if ((c >= 'a' && c <= 'z')
149 || (c >= 'A' && c <= 'Z')
150 || (c >= '0' && c <= '9')
158 static void printID(utf *u)
160 char *utf_ptr = u->text;
163 for (i = 0; i < utf_strlen(u); i++)
164 printIDpart(utf_nextu2(&utf_ptr));
168 static void addoutputsize (int len)
171 if (!dopadding) return;
173 newsize = ALIGN(outputsize, len);
175 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
176 outputsize = newsize;
179 static void printOverloadPart(utf *desc)
181 char *utf_ptr=desc->text;
185 while ((c=utf_nextu2(&utf_ptr))!=')') {
195 fprintf (file, "%c",(char)c);
202 while ( (c=utf_nextu2(&utf_ptr)) != ';')
208 default: panic ("invalid method descriptor");
213 static char *printtype(char *utf_ptr)
217 switch (utf_nextu2(&utf_ptr)) {
218 case 'V': fprintf (file, "void");
224 case 'Z': addoutputsize (4);
225 fprintf (file, "s4");
227 case 'J': addoutputsize (8);
228 fprintf (file, "s8");
230 case 'F': addoutputsize (4);
231 fprintf (file, "float");
233 case 'D': addoutputsize (8);
234 fprintf (file, "double");
237 addoutputsize ( sizeof(java_arrayheader*) );
238 switch (utf_nextu2(&utf_ptr)) {
239 case 'I': fprintf (file, "java_intarray*"); break;
240 case 'J': fprintf (file, "java_longarray*"); break;
241 case 'Z': fprintf (file, "java_booleanarray*"); break;
242 case 'B': fprintf (file, "java_bytearray*"); break;
243 case 'S': fprintf (file, "java_shortarray*"); break;
244 case 'C': fprintf (file, "java_chararray*"); break;
245 case 'F': fprintf (file, "java_floatarray*"); break;
246 case 'D': fprintf (file, "java_doublearray*"); break;
248 case '[': fprintf (file, "java_objectarray*");
249 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
251 while (utf_nextu2(&utf_ptr) != ';');
254 case 'L': fprintf (file, "java_objectarray*");
255 while ( utf_nextu2(&utf_ptr) != ';');
257 default: panic ("invalid type descriptor");
262 addoutputsize ( sizeof(java_objectheader*));
263 fprintf (file, "struct ");
264 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
268 default: panic ("Unknown type in field descriptor");
275 /***** determine the number of entries of a utf string in the ident chain *****/
277 static int searchidentchain_utf(utf *ident)
279 utf *u = chain_first(ident_chain); /* first element of list */
283 if (u==ident) count++; /* string found */
284 u = chain_next(ident_chain); /* next element in list */
291 /************** print structure for direct access to objects ******************/
293 static void printfields(classinfo *c)
300 addoutputsize(sizeof(java_objectheader));
301 fprintf(file, " java_objectheader header;\n");
305 printfields(c->super);
307 for (i = 0; i < c->fieldscount; i++) {
310 if (!(f->flags & ACC_STATIC)) {
312 printtype(f->descriptor->text);
314 utf_fprint(file, f->name);
316 /* rename multiple fieldnames */
317 if ((ident_count = searchidentchain_utf(f->name)))
318 fprintf(file, "%d", ident_count - 1);
319 chain_addlast(ident_chain, f->name);
321 fprintf(file, ";\n");
327 /***************** store prototype for native method in file ******************/
329 void printmethod(methodinfo *m)
335 /* search for return-type in descriptor */
336 utf_ptr = m->descriptor->text;
337 while (utf_nextu2(&utf_ptr) != ')');
340 fprintf(file, "\n/*\n * Class: ");
341 utf_fprint(file, m->class->name);
342 fprintf(file, "\n * Method: ");
343 utf_fprint(file, m->name);
344 fprintf(file, "\n * Signature: ");
345 utf_fprint(file, m->descriptor);
346 fprintf(file, "\n */\n");
348 /* create prototype */
349 fprintf(file, "JNIEXPORT ");
351 fprintf(file, " JNICALL Java_");
352 printID(m->class->name);
354 chain_addlast(ident_chain, m->name);
358 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
359 fprintf(file, "(JNIEnv *env");
361 utf_ptr = m->descriptor->text + 1;
363 if (!(m->flags & ACC_STATIC)) {
364 fprintf(file, ", struct ");
365 printID(m->class->name);
366 fprintf(file, "* this");
369 fprintf(file, ", jclass clazz");
372 if ((*utf_ptr) != ')') fprintf(file, ", ");
374 while ((*utf_ptr) != ')') {
375 utf_ptr = printtype(utf_ptr);
376 fprintf(file, " par%d", paramnum++);
377 if ((*utf_ptr)!=')') fprintf(file, ", ");
380 fprintf(file, ");\n\n");
384 /******* remove package-name in fully-qualified classname *********************/
386 void gen_header_filename(char *buffer, utf *u)
390 for (i = 0; i < utf_strlen(u); i++) {
391 if ((u->text[i] == '/') || (u->text[i] == '$')) {
392 buffer[i] = '_'; /* convert '$' and '/' to '_' */
395 buffer[i] = u->text[i];
398 buffer[utf_strlen(u)] = '\0';
402 /* create headerfile for classes and store native methods in chain ************/
404 void headerfile_generate(classinfo *c)
406 char header_filename[1024] = "";
407 char classname[1024];
408 char uclassname[1024];
413 u2 nativelyoverloaded;
415 /* store class in chain */
416 chain_addlast(nativeclass_chain, c);
418 /* open headerfile for class */
419 gen_header_filename(classname, c->name);
421 /* create chain for renaming fields */
422 ident_chain = chain_new();
424 sprintf(header_filename, "nat/%s.h", classname);
425 file = fopen(header_filename, "w");
426 if (!file) panic("Can not open file to store header information");
428 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
430 /* convert to uppercase */
431 for (i = 0; classname[i]; i++) {
432 uclassname[i] = toupper(classname[i]);
434 uclassname[i] = '\0';
436 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
438 /* create structure for direct access to objects */
439 fprintf(file, "/* Structure information for class: ");
440 utf_fprint(file, c->name);
441 fprintf(file, " */\n\n");
442 fprintf(file, "typedef struct ");
444 fprintf(file, " {\n");
452 fprintf(file, ";\n\n");
454 /* create chain for renaming overloaded methods */
455 chain_free(ident_chain);
456 ident_chain = chain_new();
458 /* create method-prototypes */
460 /* find overloaded methods */
461 for (i = 0; i < c->methodscount; i++) {
463 m = &(c->methods[i]);
465 if (!(m->flags & ACC_NATIVE)) continue;
466 if (!m->nativelyoverloaded) {
467 nativelyoverloaded=false;
468 for (i2=i+1;i2<c->methodscount; i2++) {
469 m2 = &(c->methods[i2]);
470 if (!(m2->flags & ACC_NATIVE)) continue;
471 if (m->name==m2->name) {
472 m2->nativelyoverloaded=true;
473 nativelyoverloaded=true;
476 m->nativelyoverloaded=nativelyoverloaded;
481 for (i = 0; i < c->methodscount; i++) {
483 m = &(c->methods[i]);
485 if (m->flags & ACC_NATIVE) {
486 chain_addlast(nativemethod_chain, m);
491 chain_free(ident_chain);
493 fprintf(file, "#endif\n\n");
499 /******** print classname, '$' used to seperate inner-class name ***********/
501 void print_classname(classinfo *clazz)
503 utf *u = clazz->name;
504 char *endpos = u->text + u->blength;
505 char *utf_ptr = u->text;
508 while (utf_ptr < endpos) {
509 if ((c = utf_nextu2(&utf_ptr)) == '_') {
519 /*************** create table for locating native functions ****************/
521 void printnativetableentry(methodinfo *m)
523 fprintf(file, " { \"");
524 print_classname(m->class);
525 fprintf(file, "\",\n \"");
526 utf_fprint(file, m->name);
527 fprintf(file, "\",\n \"");
528 utf_fprint(file, m->descriptor);
529 fprintf(file, "\",\n ");
531 if ((m->flags & ACC_STATIC) != 0)
532 fprintf(file, "true");
534 fprintf(file, "false");
536 fprintf(file, ",\n ");
537 fprintf(file, "(functionptr) Java_");
538 printID(m->class->name);
541 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
542 fprintf(file,"\n },\n");
546 void setVMClassField(classinfo *c)
551 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this) {
557 * These are local overrides for various environment variables in Emacs.
558 * Please do not remove this and leave it at the end of the file, where
559 * Emacs will automagically detect them.
560 * ---------------------------------------------------------------------
563 * indent-tabs-mode: t