1 /* src/cacaoh/headers.c - functions for header generation
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
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 2249 2005-04-06 16:06:48Z twisti $
44 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
45 # if defined(__DARWIN__)
48 # include <ucontext.h>
52 #include "mm/memory.h"
53 #include "native/include/java_lang_String.h"
54 #include "native/include/java_lang_Throwable.h"
55 #include "toolbox/chain.h"
56 #include "toolbox/logging.h"
58 #include "vm/global.h"
59 #include "vm/method.h"
60 #include "vm/tables.h"
61 #include "vm/loader.h"
62 #include "vm/options.h"
63 #include "vm/builtin.h"
64 #include "vm/jit/asmpart.h"
67 /******* replace some external functions *********/
69 functionptr native_findfunction(utf *cname, utf *mname, utf *desc, bool isstatic)
72 java_objectheader *native_new_and_init(classinfo *c) { return NULL; }
73 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s) { return NULL; }
74 java_objectheader *native_new_and_init_int(classinfo *c, s4 i) { return NULL; }
75 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t) { return NULL; }
78 java_objectheader *asm_calljavafunction(methodinfo *m, void *arg1, void *arg2,
79 void *arg3, void *arg4) { return NULL; }
81 void asm_call_jit_compiler(void) {}
83 /* assembler code patching functions */
84 void asm_builtin_new(unresolved_class *uc) {}
86 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
88 s4 asm_builtin_idiv(s4 a, s4 b) { return 0; }
89 s4 asm_builtin_irem(s4 a, s4 b) { return 0; }
90 s8 asm_builtin_ldiv(s8 a, s8 b) { return 0; }
91 s8 asm_builtin_lrem(s8 a, s8 b) { return 0; }
93 s4 asm_builtin_f2i(float a) { return 0; }
94 s8 asm_builtin_f2l(float a) { return 0; }
95 s4 asm_builtin_d2i(double a) { return 0; }
96 s8 asm_builtin_d2l(double a) { return 0; }
98 void use_class_as_object(void) {}
99 void asm_builtin_monitorenter(java_objectheader *o) {}
100 void *asm_builtin_monitorexit(java_objectheader *o) { return NULL; }
102 s4 asm_builtin_checkarraycast(java_objectheader *obj, vftbl_t *target) {
106 #if defined(__MIPS__)
107 long compare_and_swap(long *p, long oldval, long newval)
118 #if defined(__I386__)
119 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) {
122 void asm_builtin_newarray(s4 size, vftbl_t *arrayvftbl) {}
125 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
127 u1 *createcompilerstub(methodinfo *m) { return NULL; }
128 u1 *createnativestub(functionptr f, methodinfo *m) { return NULL; }
129 u1 *oldcreatenativestub(functionptr f, methodinfo *m) { return NULL; }
131 void removecompilerstub(u1 *stub) {}
132 void removenativestub(u1 *stub) {}
134 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
135 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
137 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
139 void asm_handle_builtin_exception(classinfo *c) {}
140 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
142 #if defined(__DARWIN__)
143 int cacao_catch_Handler(void) {}
146 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
147 threadcritnode asm_criticalsections;
148 void thread_restartcriticalsection(ucontext_t *uc) {}
152 void setVMClassField(classinfo *c) {}
153 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
159 typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
164 /************************ global variables **********************/
166 chain *nativemethod_chain; /* chain with native methods */
167 chain *nativeclass_chain; /* chain with processed classes */
168 static chain *ident_chain; /* chain with method and field names in current class */
170 static u4 outputsize;
171 static bool dopadding;
174 static void printIDpart(int c)
176 if ((c >= 'a' && c <= 'z')
177 || (c >= 'A' && c <= 'Z')
178 || (c >= '0' && c <= '9')
186 static void printID(utf *u)
188 char *utf_ptr = u->text;
191 for (i = 0; i < utf_strlen(u); i++)
192 printIDpart(utf_nextu2(&utf_ptr));
196 static void addoutputsize (int len)
199 if (!dopadding) return;
201 newsize = ALIGN(outputsize, len);
203 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
204 outputsize = newsize;
208 static void printOverloadPart(utf *desc)
210 char *utf_ptr=desc->text;
214 while ((c=utf_nextu2(&utf_ptr))!=')') {
224 fprintf (file, "%c",(char)c);
231 while ( (c=utf_nextu2(&utf_ptr)) != ';')
237 default: panic ("invalid method descriptor");
242 static char *printtype(char *utf_ptr)
246 switch (utf_nextu2(&utf_ptr)) {
247 case 'V': fprintf (file, "void");
253 case 'Z': addoutputsize (4);
254 fprintf (file, "s4");
256 case 'J': addoutputsize (8);
257 fprintf (file, "s8");
259 case 'F': addoutputsize (4);
260 fprintf (file, "float");
262 case 'D': addoutputsize (8);
263 fprintf (file, "double");
266 addoutputsize ( sizeof(java_arrayheader*) );
267 switch (utf_nextu2(&utf_ptr)) {
268 case 'I': fprintf (file, "java_intarray*"); break;
269 case 'J': fprintf (file, "java_longarray*"); break;
270 case 'Z': fprintf (file, "java_booleanarray*"); break;
271 case 'B': fprintf (file, "java_bytearray*"); break;
272 case 'S': fprintf (file, "java_shortarray*"); break;
273 case 'C': fprintf (file, "java_chararray*"); break;
274 case 'F': fprintf (file, "java_floatarray*"); break;
275 case 'D': fprintf (file, "java_doublearray*"); break;
277 case '[': fprintf (file, "java_objectarray*");
278 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
280 while (utf_nextu2(&utf_ptr) != ';');
283 case 'L': fprintf (file, "java_objectarray*");
284 while ( utf_nextu2(&utf_ptr) != ';');
286 default: panic ("invalid type descriptor");
291 addoutputsize ( sizeof(java_objectheader*));
292 fprintf (file, "struct ");
293 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
297 default: panic ("Unknown type in field descriptor");
304 /***** determine the number of entries of a utf string in the ident chain *****/
306 static int searchidentchain_utf(utf *ident)
308 utf *u = chain_first(ident_chain); /* first element of list */
312 if (u==ident) count++; /* string found */
313 u = chain_next(ident_chain); /* next element in list */
320 /************** print structure for direct access to objects ******************/
322 static void printfields(classinfo *c)
329 addoutputsize(sizeof(java_objectheader));
330 fprintf(file, " java_objectheader header;\n");
334 printfields(c->super.cls);
336 for (i = 0; i < c->fieldscount; i++) {
339 if (!(f->flags & ACC_STATIC)) {
341 printtype(f->descriptor->text);
343 utf_fprint(file, f->name);
345 /* rename multiple fieldnames */
346 if ((ident_count = searchidentchain_utf(f->name)))
347 fprintf(file, "%d", ident_count - 1);
348 chain_addlast(ident_chain, f->name);
350 fprintf(file, ";\n");
356 /***************** store prototype for native method in file ******************/
358 void printmethod(methodinfo *m)
363 /* search for return-type in descriptor */
364 utf_ptr = m->descriptor->text;
365 while (utf_nextu2(&utf_ptr) != ')');
368 fprintf(file, "\n/*\n * Class: ");
369 utf_fprint(file, m->class->name);
370 fprintf(file, "\n * Method: ");
371 utf_fprint(file, m->name);
372 fprintf(file, "\n * Signature: ");
373 utf_fprint(file, m->descriptor);
374 fprintf(file, "\n */\n");
376 /* create prototype */
377 fprintf(file, "JNIEXPORT ");
379 fprintf(file, " JNICALL Java_");
380 printID(m->class->name);
382 chain_addlast(ident_chain, m->name);
386 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
387 fprintf(file, "(JNIEnv *env");
389 utf_ptr = m->descriptor->text + 1;
391 if (!(m->flags & ACC_STATIC)) {
392 fprintf(file, ", struct ");
393 printID(m->class->name);
394 fprintf(file, "* this");
397 fprintf(file, ", jclass clazz");
400 if ((*utf_ptr) != ')') fprintf(file, ", ");
402 while ((*utf_ptr) != ')') {
403 utf_ptr = printtype(utf_ptr);
404 fprintf(file, " par%d", paramnum++);
405 if ((*utf_ptr)!=')') fprintf(file, ", ");
408 fprintf(file, ");\n\n");
412 /******* remove package-name in fully-qualified classname *********************/
414 void gen_header_filename(char *buffer, utf *u)
418 for (i = 0; i < utf_strlen(u); i++) {
419 if ((u->text[i] == '/') || (u->text[i] == '$')) {
420 buffer[i] = '_'; /* convert '$' and '/' to '_' */
423 buffer[i] = u->text[i];
426 buffer[utf_strlen(u)] = '\0';
430 /* create headerfile for classes and store native methods in chain ************/
432 void headerfile_generate(classinfo *c, char *opt_directory)
434 char header_filename[1024] = "";
435 char classname[1024];
436 char uclassname[1024];
441 u2 nativelyoverloaded;
443 /* store class in chain */
444 chain_addlast(nativeclass_chain, c);
446 /* open headerfile for class */
447 gen_header_filename(classname, c->name);
449 /* create chain for renaming fields */
450 ident_chain = chain_new();
453 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
456 sprintf(header_filename, "%s.h", classname);
459 file = fopen(header_filename, "w");
461 panic("Can not open file to store header information");
463 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
465 /* convert to uppercase */
466 for (i = 0; classname[i]; i++) {
467 uclassname[i] = toupper(classname[i]);
469 uclassname[i] = '\0';
471 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
473 /* create structure for direct access to objects */
474 fprintf(file, "/* Structure information for class: ");
475 utf_fprint(file, c->name);
476 fprintf(file, " */\n\n");
477 fprintf(file, "typedef struct ");
479 fprintf(file, " {\n");
487 fprintf(file, ";\n\n");
489 /* create chain for renaming overloaded methods */
490 chain_free(ident_chain);
491 ident_chain = chain_new();
493 /* create method-prototypes */
495 /* find overloaded methods */
496 for (i = 0; i < c->methodscount; i++) {
498 m = &(c->methods[i]);
500 if (!(m->flags & ACC_NATIVE)) continue;
501 if (!m->nativelyoverloaded) {
502 nativelyoverloaded=false;
503 for (i2=i+1;i2<c->methodscount; i2++) {
504 m2 = &(c->methods[i2]);
505 if (!(m2->flags & ACC_NATIVE)) continue;
506 if (m->name==m2->name) {
507 m2->nativelyoverloaded=true;
508 nativelyoverloaded=true;
511 m->nativelyoverloaded=nativelyoverloaded;
516 for (i = 0; i < c->methodscount; i++) {
518 m = &(c->methods[i]);
520 if (m->flags & ACC_NATIVE) {
521 chain_addlast(nativemethod_chain, m);
526 chain_free(ident_chain);
528 fprintf(file, "#endif\n\n");
534 /******** print classname, '$' used to seperate inner-class name ***********/
536 void print_classname(classinfo *clazz)
538 utf *u = clazz->name;
539 char *endpos = u->text + u->blength;
540 char *utf_ptr = u->text;
543 while (utf_ptr < endpos) {
544 if ((c = utf_nextu2(&utf_ptr)) == '_') {
555 * These are local overrides for various environment variables in Emacs.
556 * Please do not remove this and leave it at the end of the file, where
557 * Emacs will automagically detect them.
558 * ---------------------------------------------------------------------
561 * indent-tabs-mode: t