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 2447 2005-05-11 12:55:06Z 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 /* code patching functions */
84 void patcher_builtin_new(u1 *sp) {}
85 void patcher_builtin_newarray(u1 *sp) {}
86 void patcher_builtin_arraycheckcast(u1 *sp) {}
87 void patcher_builtin_arrayinstanceof(u1 *sp) {}
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(void) {}
100 void asm_builtin_monitorenter(java_objectheader *o) {}
101 void *asm_builtin_monitorexit(java_objectheader *o) { return NULL; }
103 s4 asm_builtin_arraycheckcast(java_objectheader *obj, vftbl_t *target)
108 #if defined(__MIPS__)
109 long compare_and_swap(long *p, long oldval, long newval)
120 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
122 u1 *createcompilerstub(methodinfo *m) { return NULL; }
123 u1 *createnativestub(functionptr f, methodinfo *m) { return NULL; }
124 u1 *oldcreatenativestub(functionptr f, methodinfo *m) { return NULL; }
126 void removecompilerstub(u1 *stub) {}
127 void removenativestub(u1 *stub) {}
129 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
130 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
132 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
134 void asm_handle_builtin_exception(classinfo *c) {}
135 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
137 #if defined(__DARWIN__)
138 int cacao_catch_Handler(void) {}
141 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
142 threadcritnode asm_criticalsections;
143 void thread_restartcriticalsection(ucontext_t *uc) {}
147 void setVMClassField(classinfo *c) {}
148 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
154 typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
159 /************************ global variables **********************/
161 chain *nativemethod_chain; /* chain with native methods */
162 chain *nativeclass_chain; /* chain with processed classes */
163 static chain *ident_chain; /* chain with method and field names in current class */
165 static u4 outputsize;
166 static bool dopadding;
169 static void printIDpart(int c)
171 if ((c >= 'a' && c <= 'z')
172 || (c >= 'A' && c <= 'Z')
173 || (c >= '0' && c <= '9')
181 static void printID(utf *u)
183 char *utf_ptr = u->text;
186 for (i = 0; i < utf_strlen(u); i++)
187 printIDpart(utf_nextu2(&utf_ptr));
191 static void addoutputsize (int len)
194 if (!dopadding) return;
196 newsize = ALIGN(outputsize, len);
198 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
199 outputsize = newsize;
203 static void printOverloadPart(utf *desc)
205 char *utf_ptr=desc->text;
209 while ((c=utf_nextu2(&utf_ptr))!=')') {
219 fprintf (file, "%c",(char)c);
226 while ( (c=utf_nextu2(&utf_ptr)) != ';')
232 default: panic ("invalid method descriptor");
237 static char *printtype(char *utf_ptr)
241 switch (utf_nextu2(&utf_ptr)) {
242 case 'V': fprintf (file, "void");
248 case 'Z': addoutputsize (4);
249 fprintf (file, "s4");
251 case 'J': addoutputsize (8);
252 fprintf (file, "s8");
254 case 'F': addoutputsize (4);
255 fprintf (file, "float");
257 case 'D': addoutputsize (8);
258 fprintf (file, "double");
261 addoutputsize ( sizeof(java_arrayheader*) );
262 switch (utf_nextu2(&utf_ptr)) {
263 case 'I': fprintf (file, "java_intarray*"); break;
264 case 'J': fprintf (file, "java_longarray*"); break;
265 case 'Z': fprintf (file, "java_booleanarray*"); break;
266 case 'B': fprintf (file, "java_bytearray*"); break;
267 case 'S': fprintf (file, "java_shortarray*"); break;
268 case 'C': fprintf (file, "java_chararray*"); break;
269 case 'F': fprintf (file, "java_floatarray*"); break;
270 case 'D': fprintf (file, "java_doublearray*"); break;
272 case '[': fprintf (file, "java_objectarray*");
273 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
275 while (utf_nextu2(&utf_ptr) != ';');
278 case 'L': fprintf (file, "java_objectarray*");
279 while ( utf_nextu2(&utf_ptr) != ';');
281 default: panic ("invalid type descriptor");
286 addoutputsize ( sizeof(java_objectheader*));
287 fprintf (file, "struct ");
288 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
292 default: panic ("Unknown type in field descriptor");
299 /***** determine the number of entries of a utf string in the ident chain *****/
301 static int searchidentchain_utf(utf *ident)
303 utf *u = chain_first(ident_chain); /* first element of list */
307 if (u==ident) count++; /* string found */
308 u = chain_next(ident_chain); /* next element in list */
315 /************** print structure for direct access to objects ******************/
317 static void printfields(classinfo *c)
324 addoutputsize(sizeof(java_objectheader));
325 fprintf(file, " java_objectheader header;\n");
329 printfields(c->super.cls);
331 for (i = 0; i < c->fieldscount; i++) {
334 if (!(f->flags & ACC_STATIC)) {
336 printtype(f->descriptor->text);
338 utf_fprint(file, f->name);
340 /* rename multiple fieldnames */
341 if ((ident_count = searchidentchain_utf(f->name)))
342 fprintf(file, "%d", ident_count - 1);
343 chain_addlast(ident_chain, f->name);
345 fprintf(file, ";\n");
351 /***************** store prototype for native method in file ******************/
353 void printmethod(methodinfo *m)
358 /* search for return-type in descriptor */
359 utf_ptr = m->descriptor->text;
360 while (utf_nextu2(&utf_ptr) != ')');
363 fprintf(file, "\n/*\n * Class: ");
364 utf_fprint(file, m->class->name);
365 fprintf(file, "\n * Method: ");
366 utf_fprint(file, m->name);
367 fprintf(file, "\n * Signature: ");
368 utf_fprint(file, m->descriptor);
369 fprintf(file, "\n */\n");
371 /* create prototype */
372 fprintf(file, "JNIEXPORT ");
374 fprintf(file, " JNICALL Java_");
375 printID(m->class->name);
377 chain_addlast(ident_chain, m->name);
381 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
382 fprintf(file, "(JNIEnv *env");
384 utf_ptr = m->descriptor->text + 1;
386 if (!(m->flags & ACC_STATIC)) {
387 fprintf(file, ", struct ");
388 printID(m->class->name);
389 fprintf(file, "* this");
392 fprintf(file, ", jclass clazz");
395 if ((*utf_ptr) != ')') fprintf(file, ", ");
397 while ((*utf_ptr) != ')') {
398 utf_ptr = printtype(utf_ptr);
399 fprintf(file, " par%d", paramnum++);
400 if ((*utf_ptr)!=')') fprintf(file, ", ");
403 fprintf(file, ");\n\n");
407 /******* remove package-name in fully-qualified classname *********************/
409 void gen_header_filename(char *buffer, utf *u)
413 for (i = 0; i < utf_strlen(u); i++) {
414 if ((u->text[i] == '/') || (u->text[i] == '$')) {
415 buffer[i] = '_'; /* convert '$' and '/' to '_' */
418 buffer[i] = u->text[i];
421 buffer[utf_strlen(u)] = '\0';
425 /* create headerfile for classes and store native methods in chain ************/
427 void headerfile_generate(classinfo *c, char *opt_directory)
429 char header_filename[1024] = "";
430 char classname[1024];
431 char uclassname[1024];
436 u2 nativelyoverloaded;
438 /* store class in chain */
439 chain_addlast(nativeclass_chain, c);
441 /* open headerfile for class */
442 gen_header_filename(classname, c->name);
444 /* create chain for renaming fields */
445 ident_chain = chain_new();
448 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
451 sprintf(header_filename, "%s.h", classname);
454 file = fopen(header_filename, "w");
456 panic("Can not open file to store header information");
458 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
460 /* convert to uppercase */
461 for (i = 0; classname[i]; i++) {
462 uclassname[i] = toupper(classname[i]);
464 uclassname[i] = '\0';
466 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
468 /* create structure for direct access to objects */
469 fprintf(file, "/* Structure information for class: ");
470 utf_fprint(file, c->name);
471 fprintf(file, " */\n\n");
472 fprintf(file, "typedef struct ");
474 fprintf(file, " {\n");
482 fprintf(file, ";\n\n");
484 /* create chain for renaming overloaded methods */
485 chain_free(ident_chain);
486 ident_chain = chain_new();
488 /* create method-prototypes */
490 /* find overloaded methods */
491 for (i = 0; i < c->methodscount; i++) {
493 m = &(c->methods[i]);
495 if (!(m->flags & ACC_NATIVE)) continue;
496 if (!m->nativelyoverloaded) {
497 nativelyoverloaded=false;
498 for (i2=i+1;i2<c->methodscount; i2++) {
499 m2 = &(c->methods[i2]);
500 if (!(m2->flags & ACC_NATIVE)) continue;
501 if (m->name==m2->name) {
502 m2->nativelyoverloaded=true;
503 nativelyoverloaded=true;
506 m->nativelyoverloaded=nativelyoverloaded;
511 for (i = 0; i < c->methodscount; i++) {
513 m = &(c->methods[i]);
515 if (m->flags & ACC_NATIVE) {
516 chain_addlast(nativemethod_chain, m);
521 chain_free(ident_chain);
523 fprintf(file, "#endif\n\n");
529 /******** print classname, '$' used to seperate inner-class name ***********/
531 void print_classname(classinfo *clazz)
533 utf *u = clazz->name;
534 char *endpos = u->text + u->blength;
535 char *utf_ptr = u->text;
538 while (utf_ptr < endpos) {
539 if ((c = utf_nextu2(&utf_ptr)) == '_') {
550 * These are local overrides for various environment variables in Emacs.
551 * Please do not remove this and leave it at the end of the file, where
552 * Emacs will automagically detect them.
553 * ---------------------------------------------------------------------
556 * indent-tabs-mode: t