1 /* vm/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 1633 2004-12-01 09:45:33Z twisti $
42 #if defined(__DARWIN__)
49 #include "mm/memory.h"
50 #include "native/include/java_lang_String.h"
51 #include "native/include/java_lang_Throwable.h"
52 #include "toolbox/chain.h"
53 #include "toolbox/logging.h"
54 #include "vm/global.h"
55 #include "vm/tables.h"
56 #include "vm/loader.h"
57 #include "vm/options.h"
58 #include "vm/builtin.h"
61 /******* replace some external functions *********/
63 functionptr native_findfunction(utf *cname, utf *mname, utf *desc, bool isstatic)
66 java_objectheader *javastring_new(utf *text) { return NULL; }
67 java_objectheader *javastring_new_char(char *text) { return NULL; }
69 char *javastring_tochar(java_objectheader *so) { return NULL; }
70 utf *javastring_toutf(java_lang_String *string, bool isclassname)
74 java_objectheader *native_new_and_init(classinfo *c) { return NULL; }
75 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s) { return NULL; }
76 java_objectheader *native_new_and_init_int(classinfo *c, s4 i) { return NULL; }
77 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t) { return NULL; }
79 java_objectheader *literalstring_new(utf *u) { return NULL; }
82 void literalstring_free(java_objectheader *o) {}
83 void stringtable_update() { }
84 void synchronize_caches() { }
85 void asm_call_jit_compiler() { }
86 void asm_calljavafunction() { }
87 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
89 s4 asm_builtin_idiv(s4 a, s4 b) {return 0;}
90 s4 asm_builtin_irem(s4 a, s4 b) {return 0;}
91 s8 asm_builtin_ldiv(s8 a, s8 b) {return 0;}
92 s8 asm_builtin_lrem(s8 a, s8 b) {return 0;}
94 s4 asm_builtin_f2i(float a) { return 0; }
95 s8 asm_builtin_f2l(float a) { return 0; }
96 s4 asm_builtin_d2i(double a) { return 0; }
97 s8 asm_builtin_d2l(double a) { return 0; }
99 void use_class_as_object() {}
100 void asm_builtin_monitorenter(java_objectheader *o) {}
101 void *asm_builtin_monitorexit(java_objectheader *o) { return NULL; }
103 s4 asm_builtin_checkarraycast(java_objectheader *obj, vftbl_t *target) {return 0;}
105 #if defined(__MIPS__)
106 long compare_and_swap(long *p, long oldval, long newval)
117 #if defined(__I386__)
118 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) { return 0; }
119 void asm_builtin_newarray(s4 size, vftbl_t *arrayvftbl) {}
122 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
124 u1 *createcompilerstub(methodinfo *m) {return NULL;}
125 u1 *createnativestub(functionptr f, methodinfo *m) {return NULL;}
126 u1 *oldcreatenativestub(functionptr f, methodinfo *m) {return NULL;}
128 void removecompilerstub(u1 *stub) {}
129 void removenativestub(u1 *stub) {}
131 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
132 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
133 void thread_restartcriticalsection(ucontext_t *uc) {}
134 void asm_switchstackandcall() {}
135 void asm_handle_builtin_exception(classinfo *c) {}
136 void asm_getclassvalues_atomic() {}
138 #if defined(__DARWIN__)
139 int cacao_catch_Handler() {}
142 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
143 threadcritnode asm_criticalsections;
147 /************************ global variables **********************/
149 THREADSPECIFIC java_objectheader *_exceptionptr;
151 chain *nativemethod_chain; /* chain with native methods */
152 chain *nativeclass_chain; /* chain with processed classes */
153 static chain *ident_chain; /* chain with method and field names in current class */
155 static u4 outputsize;
156 static bool dopadding;
159 static void printIDpart(int c)
161 if ((c >= 'a' && c <= 'z')
162 || (c >= 'A' && c <= 'Z')
163 || (c >= '0' && c <= '9')
171 static void printID(utf *u)
173 char *utf_ptr = u->text;
176 for (i = 0; i < utf_strlen(u); i++)
177 printIDpart(utf_nextu2(&utf_ptr));
181 static void addoutputsize (int len)
184 if (!dopadding) return;
186 newsize = ALIGN(outputsize, len);
188 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
189 outputsize = newsize;
192 static void printOverloadPart(utf *desc)
194 char *utf_ptr=desc->text;
198 while ((c=utf_nextu2(&utf_ptr))!=')') {
208 fprintf (file, "%c",(char)c);
215 while ( (c=utf_nextu2(&utf_ptr)) != ';')
221 default: panic ("invalid method descriptor");
226 static char *printtype(char *utf_ptr)
230 switch (utf_nextu2(&utf_ptr)) {
231 case 'V': fprintf (file, "void");
237 case 'Z': addoutputsize (4);
238 fprintf (file, "s4");
240 case 'J': addoutputsize (8);
241 fprintf (file, "s8");
243 case 'F': addoutputsize (4);
244 fprintf (file, "float");
246 case 'D': addoutputsize (8);
247 fprintf (file, "double");
250 addoutputsize ( sizeof(java_arrayheader*) );
251 switch (utf_nextu2(&utf_ptr)) {
252 case 'I': fprintf (file, "java_intarray*"); break;
253 case 'J': fprintf (file, "java_longarray*"); break;
254 case 'Z': fprintf (file, "java_booleanarray*"); break;
255 case 'B': fprintf (file, "java_bytearray*"); break;
256 case 'S': fprintf (file, "java_shortarray*"); break;
257 case 'C': fprintf (file, "java_chararray*"); break;
258 case 'F': fprintf (file, "java_floatarray*"); break;
259 case 'D': fprintf (file, "java_doublearray*"); break;
261 case '[': fprintf (file, "java_objectarray*");
262 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
264 while (utf_nextu2(&utf_ptr) != ';');
267 case 'L': fprintf (file, "java_objectarray*");
268 while ( utf_nextu2(&utf_ptr) != ';');
270 default: panic ("invalid type descriptor");
275 addoutputsize ( sizeof(java_objectheader*));
276 fprintf (file, "struct ");
277 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
281 default: panic ("Unknown type in field descriptor");
288 /***** determine the number of entries of a utf string in the ident chain *****/
290 static int searchidentchain_utf(utf *ident)
292 utf *u = chain_first(ident_chain); /* first element of list */
296 if (u==ident) count++; /* string found */
297 u = chain_next(ident_chain); /* next element in list */
304 /************** print structure for direct access to objects ******************/
306 static void printfields(classinfo *c)
313 addoutputsize(sizeof(java_objectheader));
314 fprintf(file, " java_objectheader header;\n");
318 printfields(c->super);
320 for (i = 0; i < c->fieldscount; i++) {
323 if (!(f->flags & ACC_STATIC)) {
325 printtype(f->descriptor->text);
327 utf_fprint(file, f->name);
329 /* rename multiple fieldnames */
330 if ((ident_count = searchidentchain_utf(f->name)))
331 fprintf(file, "%d", ident_count - 1);
332 chain_addlast(ident_chain, f->name);
334 fprintf(file, ";\n");
340 /***************** store prototype for native method in file ******************/
342 void printmethod(methodinfo *m)
347 /* search for return-type in descriptor */
348 utf_ptr = m->descriptor->text;
349 while (utf_nextu2(&utf_ptr) != ')');
352 fprintf(file, "\n/*\n * Class: ");
353 utf_fprint(file, m->class->name);
354 fprintf(file, "\n * Method: ");
355 utf_fprint(file, m->name);
356 fprintf(file, "\n * Signature: ");
357 utf_fprint(file, m->descriptor);
358 fprintf(file, "\n */\n");
360 /* create prototype */
361 fprintf(file, "JNIEXPORT ");
363 fprintf(file, " JNICALL Java_");
364 printID(m->class->name);
366 chain_addlast(ident_chain, m->name);
370 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
371 fprintf(file, "(JNIEnv *env");
373 utf_ptr = m->descriptor->text + 1;
375 if (!(m->flags & ACC_STATIC)) {
376 fprintf(file, ", struct ");
377 printID(m->class->name);
378 fprintf(file, "* this");
381 fprintf(file, ", jclass clazz");
384 if ((*utf_ptr) != ')') fprintf(file, ", ");
386 while ((*utf_ptr) != ')') {
387 utf_ptr = printtype(utf_ptr);
388 fprintf(file, " par%d", paramnum++);
389 if ((*utf_ptr)!=')') fprintf(file, ", ");
392 fprintf(file, ");\n\n");
396 /******* remove package-name in fully-qualified classname *********************/
398 void gen_header_filename(char *buffer, utf *u)
402 for (i = 0; i < utf_strlen(u); i++) {
403 if ((u->text[i] == '/') || (u->text[i] == '$')) {
404 buffer[i] = '_'; /* convert '$' and '/' to '_' */
407 buffer[i] = u->text[i];
410 buffer[utf_strlen(u)] = '\0';
414 /* create headerfile for classes and store native methods in chain ************/
416 void headerfile_generate(classinfo *c, char *opt_directory)
418 char header_filename[1024] = "";
419 char classname[1024];
420 char uclassname[1024];
425 u2 nativelyoverloaded;
427 /* store class in chain */
428 chain_addlast(nativeclass_chain, c);
430 /* open headerfile for class */
431 gen_header_filename(classname, c->name);
433 /* create chain for renaming fields */
434 ident_chain = chain_new();
437 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
440 sprintf(header_filename, "%s.h", classname);
443 file = fopen(header_filename, "w");
445 panic("Can not open file to store header information");
447 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
449 /* convert to uppercase */
450 for (i = 0; classname[i]; i++) {
451 uclassname[i] = toupper(classname[i]);
453 uclassname[i] = '\0';
455 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
457 /* create structure for direct access to objects */
458 fprintf(file, "/* Structure information for class: ");
459 utf_fprint(file, c->name);
460 fprintf(file, " */\n\n");
461 fprintf(file, "typedef struct ");
463 fprintf(file, " {\n");
471 fprintf(file, ";\n\n");
473 /* create chain for renaming overloaded methods */
474 chain_free(ident_chain);
475 ident_chain = chain_new();
477 /* create method-prototypes */
479 /* find overloaded methods */
480 for (i = 0; i < c->methodscount; i++) {
482 m = &(c->methods[i]);
484 if (!(m->flags & ACC_NATIVE)) continue;
485 if (!m->nativelyoverloaded) {
486 nativelyoverloaded=false;
487 for (i2=i+1;i2<c->methodscount; i2++) {
488 m2 = &(c->methods[i2]);
489 if (!(m2->flags & ACC_NATIVE)) continue;
490 if (m->name==m2->name) {
491 m2->nativelyoverloaded=true;
492 nativelyoverloaded=true;
495 m->nativelyoverloaded=nativelyoverloaded;
500 for (i = 0; i < c->methodscount; i++) {
502 m = &(c->methods[i]);
504 if (m->flags & ACC_NATIVE) {
505 chain_addlast(nativemethod_chain, m);
510 chain_free(ident_chain);
512 fprintf(file, "#endif\n\n");
518 /******** print classname, '$' used to seperate inner-class name ***********/
520 void print_classname(classinfo *clazz)
522 utf *u = clazz->name;
523 char *endpos = u->text + u->blength;
524 char *utf_ptr = u->text;
527 while (utf_ptr < endpos) {
528 if ((c = utf_nextu2(&utf_ptr)) == '_') {
538 /*************** create table for locating native functions ****************/
540 void printnativetableentry(methodinfo *m)
542 fprintf(file, " { \"");
543 print_classname(m->class);
544 fprintf(file, "\",\n \"");
545 utf_fprint(file, m->name);
546 fprintf(file, "\",\n \"");
547 utf_fprint(file, m->descriptor);
548 fprintf(file, "\",\n ");
550 if ((m->flags & ACC_STATIC) != 0)
551 fprintf(file, "true");
553 fprintf(file, "false");
555 fprintf(file, ",\n ");
556 fprintf(file, "(functionptr) Java_");
557 printID(m->class->name);
560 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
561 fprintf(file,"\n },\n");
565 void setVMClassField(classinfo *c)
570 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this) {
576 * These are local overrides for various environment variables in Emacs.
577 * Please do not remove this and leave it at the end of the file, where
578 * Emacs will automagically detect them.
579 * ---------------------------------------------------------------------
582 * indent-tabs-mode: t