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 2193 2005-04-02 19:33:43Z edwin $
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 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
85 s4 asm_builtin_idiv(s4 a, s4 b) { return 0; }
86 s4 asm_builtin_irem(s4 a, s4 b) { return 0; }
87 s8 asm_builtin_ldiv(s8 a, s8 b) { return 0; }
88 s8 asm_builtin_lrem(s8 a, s8 b) { return 0; }
90 s4 asm_builtin_f2i(float a) { return 0; }
91 s8 asm_builtin_f2l(float a) { return 0; }
92 s4 asm_builtin_d2i(double a) { return 0; }
93 s8 asm_builtin_d2l(double a) { return 0; }
95 void use_class_as_object(void) {}
96 void asm_builtin_monitorenter(java_objectheader *o) {}
97 void *asm_builtin_monitorexit(java_objectheader *o) { return NULL; }
99 s4 asm_builtin_checkarraycast(java_objectheader *obj, vftbl_t *target) {
103 #if defined(__MIPS__)
104 long compare_and_swap(long *p, long oldval, long newval)
115 java_objectheader *asm_builtin_new(classinfo *c) { return NULL; }
117 #if defined(__I386__)
118 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) {
121 void asm_builtin_newarray(s4 size, vftbl_t *arrayvftbl) {}
124 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
126 u1 *createcompilerstub(methodinfo *m) { return NULL; }
127 u1 *createnativestub(functionptr f, methodinfo *m) { return NULL; }
128 u1 *oldcreatenativestub(functionptr f, methodinfo *m) { return NULL; }
130 void removecompilerstub(u1 *stub) {}
131 void removenativestub(u1 *stub) {}
133 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
134 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
136 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
138 void asm_handle_builtin_exception(classinfo *c) {}
139 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
141 #if defined(__DARWIN__)
142 int cacao_catch_Handler(void) {}
145 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
146 threadcritnode asm_criticalsections;
147 void thread_restartcriticalsection(ucontext_t *uc) {}
151 void setVMClassField(classinfo *c) {}
152 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
158 typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
163 /************************ global variables **********************/
165 chain *nativemethod_chain; /* chain with native methods */
166 chain *nativeclass_chain; /* chain with processed classes */
167 static chain *ident_chain; /* chain with method and field names in current class */
169 static u4 outputsize;
170 static bool dopadding;
173 static void printIDpart(int c)
175 if ((c >= 'a' && c <= 'z')
176 || (c >= 'A' && c <= 'Z')
177 || (c >= '0' && c <= '9')
185 static void printID(utf *u)
187 char *utf_ptr = u->text;
190 for (i = 0; i < utf_strlen(u); i++)
191 printIDpart(utf_nextu2(&utf_ptr));
195 static void addoutputsize (int len)
198 if (!dopadding) return;
200 newsize = ALIGN(outputsize, len);
202 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
203 outputsize = newsize;
207 static void printOverloadPart(utf *desc)
209 char *utf_ptr=desc->text;
213 while ((c=utf_nextu2(&utf_ptr))!=')') {
223 fprintf (file, "%c",(char)c);
230 while ( (c=utf_nextu2(&utf_ptr)) != ';')
236 default: panic ("invalid method descriptor");
241 static char *printtype(char *utf_ptr)
245 switch (utf_nextu2(&utf_ptr)) {
246 case 'V': fprintf (file, "void");
252 case 'Z': addoutputsize (4);
253 fprintf (file, "s4");
255 case 'J': addoutputsize (8);
256 fprintf (file, "s8");
258 case 'F': addoutputsize (4);
259 fprintf (file, "float");
261 case 'D': addoutputsize (8);
262 fprintf (file, "double");
265 addoutputsize ( sizeof(java_arrayheader*) );
266 switch (utf_nextu2(&utf_ptr)) {
267 case 'I': fprintf (file, "java_intarray*"); break;
268 case 'J': fprintf (file, "java_longarray*"); break;
269 case 'Z': fprintf (file, "java_booleanarray*"); break;
270 case 'B': fprintf (file, "java_bytearray*"); break;
271 case 'S': fprintf (file, "java_shortarray*"); break;
272 case 'C': fprintf (file, "java_chararray*"); break;
273 case 'F': fprintf (file, "java_floatarray*"); break;
274 case 'D': fprintf (file, "java_doublearray*"); break;
276 case '[': fprintf (file, "java_objectarray*");
277 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
279 while (utf_nextu2(&utf_ptr) != ';');
282 case 'L': fprintf (file, "java_objectarray*");
283 while ( utf_nextu2(&utf_ptr) != ';');
285 default: panic ("invalid type descriptor");
290 addoutputsize ( sizeof(java_objectheader*));
291 fprintf (file, "struct ");
292 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
296 default: panic ("Unknown type in field descriptor");
303 /***** determine the number of entries of a utf string in the ident chain *****/
305 static int searchidentchain_utf(utf *ident)
307 utf *u = chain_first(ident_chain); /* first element of list */
311 if (u==ident) count++; /* string found */
312 u = chain_next(ident_chain); /* next element in list */
319 /************** print structure for direct access to objects ******************/
321 static void printfields(classinfo *c)
328 addoutputsize(sizeof(java_objectheader));
329 fprintf(file, " java_objectheader header;\n");
333 printfields(c->super.cls);
335 for (i = 0; i < c->fieldscount; i++) {
338 if (!(f->flags & ACC_STATIC)) {
340 printtype(f->descriptor->text);
342 utf_fprint(file, f->name);
344 /* rename multiple fieldnames */
345 if ((ident_count = searchidentchain_utf(f->name)))
346 fprintf(file, "%d", ident_count - 1);
347 chain_addlast(ident_chain, f->name);
349 fprintf(file, ";\n");
355 /***************** store prototype for native method in file ******************/
357 void printmethod(methodinfo *m)
362 /* search for return-type in descriptor */
363 utf_ptr = m->descriptor->text;
364 while (utf_nextu2(&utf_ptr) != ')');
367 fprintf(file, "\n/*\n * Class: ");
368 utf_fprint(file, m->class->name);
369 fprintf(file, "\n * Method: ");
370 utf_fprint(file, m->name);
371 fprintf(file, "\n * Signature: ");
372 utf_fprint(file, m->descriptor);
373 fprintf(file, "\n */\n");
375 /* create prototype */
376 fprintf(file, "JNIEXPORT ");
378 fprintf(file, " JNICALL Java_");
379 printID(m->class->name);
381 chain_addlast(ident_chain, m->name);
385 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
386 fprintf(file, "(JNIEnv *env");
388 utf_ptr = m->descriptor->text + 1;
390 if (!(m->flags & ACC_STATIC)) {
391 fprintf(file, ", struct ");
392 printID(m->class->name);
393 fprintf(file, "* this");
396 fprintf(file, ", jclass clazz");
399 if ((*utf_ptr) != ')') fprintf(file, ", ");
401 while ((*utf_ptr) != ')') {
402 utf_ptr = printtype(utf_ptr);
403 fprintf(file, " par%d", paramnum++);
404 if ((*utf_ptr)!=')') fprintf(file, ", ");
407 fprintf(file, ");\n\n");
411 /******* remove package-name in fully-qualified classname *********************/
413 void gen_header_filename(char *buffer, utf *u)
417 for (i = 0; i < utf_strlen(u); i++) {
418 if ((u->text[i] == '/') || (u->text[i] == '$')) {
419 buffer[i] = '_'; /* convert '$' and '/' to '_' */
422 buffer[i] = u->text[i];
425 buffer[utf_strlen(u)] = '\0';
429 /* create headerfile for classes and store native methods in chain ************/
431 void headerfile_generate(classinfo *c, char *opt_directory)
433 char header_filename[1024] = "";
434 char classname[1024];
435 char uclassname[1024];
440 u2 nativelyoverloaded;
442 /* store class in chain */
443 chain_addlast(nativeclass_chain, c);
445 /* open headerfile for class */
446 gen_header_filename(classname, c->name);
448 /* create chain for renaming fields */
449 ident_chain = chain_new();
452 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
455 sprintf(header_filename, "%s.h", classname);
458 file = fopen(header_filename, "w");
460 panic("Can not open file to store header information");
462 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
464 /* convert to uppercase */
465 for (i = 0; classname[i]; i++) {
466 uclassname[i] = toupper(classname[i]);
468 uclassname[i] = '\0';
470 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
472 /* create structure for direct access to objects */
473 fprintf(file, "/* Structure information for class: ");
474 utf_fprint(file, c->name);
475 fprintf(file, " */\n\n");
476 fprintf(file, "typedef struct ");
478 fprintf(file, " {\n");
486 fprintf(file, ";\n\n");
488 /* create chain for renaming overloaded methods */
489 chain_free(ident_chain);
490 ident_chain = chain_new();
492 /* create method-prototypes */
494 /* find overloaded methods */
495 for (i = 0; i < c->methodscount; i++) {
497 m = &(c->methods[i]);
499 if (!(m->flags & ACC_NATIVE)) continue;
500 if (!m->nativelyoverloaded) {
501 nativelyoverloaded=false;
502 for (i2=i+1;i2<c->methodscount; i2++) {
503 m2 = &(c->methods[i2]);
504 if (!(m2->flags & ACC_NATIVE)) continue;
505 if (m->name==m2->name) {
506 m2->nativelyoverloaded=true;
507 nativelyoverloaded=true;
510 m->nativelyoverloaded=nativelyoverloaded;
515 for (i = 0; i < c->methodscount; i++) {
517 m = &(c->methods[i]);
519 if (m->flags & ACC_NATIVE) {
520 chain_addlast(nativemethod_chain, m);
525 chain_free(ident_chain);
527 fprintf(file, "#endif\n\n");
533 /******** print classname, '$' used to seperate inner-class name ***********/
535 void print_classname(classinfo *clazz)
537 utf *u = clazz->name;
538 char *endpos = u->text + u->blength;
539 char *utf_ptr = u->text;
542 while (utf_ptr < endpos) {
543 if ((c = utf_nextu2(&utf_ptr)) == '_') {
554 * These are local overrides for various environment variables in Emacs.
555 * Please do not remove this and leave it at the end of the file, where
556 * Emacs will automagically detect them.
557 * ---------------------------------------------------------------------
560 * indent-tabs-mode: t