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 2328 2005-04-21 22:38:17Z 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_wrapper_patcher_builtin_new(unresolved_class *uc) {}
85 void asm_wrapper_patcher_builtin_newarray(unresolved_class *uc) {}
86 void asm_wrapper_patcher_builtin_checkarraycast(unresolved_class *uc) {}
87 void asm_wrapper_patcher_builtin_arrayinstanceof(unresolved_class *uc) {}
89 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
91 s4 asm_builtin_idiv(s4 a, s4 b) { return 0; }
92 s4 asm_builtin_irem(s4 a, s4 b) { return 0; }
93 s8 asm_builtin_ldiv(s8 a, s8 b) { return 0; }
94 s8 asm_builtin_lrem(s8 a, s8 b) { return 0; }
96 s4 asm_builtin_f2i(float a) { return 0; }
97 s8 asm_builtin_f2l(float a) { return 0; }
98 s4 asm_builtin_d2i(double a) { return 0; }
99 s8 asm_builtin_d2l(double a) { return 0; }
101 void use_class_as_object(void) {}
102 void asm_builtin_monitorenter(java_objectheader *o) {}
103 void *asm_builtin_monitorexit(java_objectheader *o) { return NULL; }
105 #if defined(__I386__)
106 void asm_builtin_newarray(s4 size, vftbl_t *arrayvftbl) {}
109 s4 asm_builtin_checkarraycast(java_objectheader *obj, vftbl_t *target) {
113 #if defined(__MIPS__)
114 long compare_and_swap(long *p, long oldval, long newval)
125 #if defined(__I386__)
126 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) {
131 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
133 u1 *createcompilerstub(methodinfo *m) { return NULL; }
134 u1 *createnativestub(functionptr f, methodinfo *m) { return NULL; }
135 u1 *oldcreatenativestub(functionptr f, methodinfo *m) { return NULL; }
137 void removecompilerstub(u1 *stub) {}
138 void removenativestub(u1 *stub) {}
140 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
141 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
143 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
145 void asm_handle_builtin_exception(classinfo *c) {}
146 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
148 #if defined(__DARWIN__)
149 int cacao_catch_Handler(void) {}
152 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
153 threadcritnode asm_criticalsections;
154 void thread_restartcriticalsection(ucontext_t *uc) {}
158 void setVMClassField(classinfo *c) {}
159 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
165 typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
170 /************************ global variables **********************/
172 chain *nativemethod_chain; /* chain with native methods */
173 chain *nativeclass_chain; /* chain with processed classes */
174 static chain *ident_chain; /* chain with method and field names in current class */
176 static u4 outputsize;
177 static bool dopadding;
180 static void printIDpart(int c)
182 if ((c >= 'a' && c <= 'z')
183 || (c >= 'A' && c <= 'Z')
184 || (c >= '0' && c <= '9')
192 static void printID(utf *u)
194 char *utf_ptr = u->text;
197 for (i = 0; i < utf_strlen(u); i++)
198 printIDpart(utf_nextu2(&utf_ptr));
202 static void addoutputsize (int len)
205 if (!dopadding) return;
207 newsize = ALIGN(outputsize, len);
209 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
210 outputsize = newsize;
214 static void printOverloadPart(utf *desc)
216 char *utf_ptr=desc->text;
220 while ((c=utf_nextu2(&utf_ptr))!=')') {
230 fprintf (file, "%c",(char)c);
237 while ( (c=utf_nextu2(&utf_ptr)) != ';')
243 default: panic ("invalid method descriptor");
248 static char *printtype(char *utf_ptr)
252 switch (utf_nextu2(&utf_ptr)) {
253 case 'V': fprintf (file, "void");
259 case 'Z': addoutputsize (4);
260 fprintf (file, "s4");
262 case 'J': addoutputsize (8);
263 fprintf (file, "s8");
265 case 'F': addoutputsize (4);
266 fprintf (file, "float");
268 case 'D': addoutputsize (8);
269 fprintf (file, "double");
272 addoutputsize ( sizeof(java_arrayheader*) );
273 switch (utf_nextu2(&utf_ptr)) {
274 case 'I': fprintf (file, "java_intarray*"); break;
275 case 'J': fprintf (file, "java_longarray*"); break;
276 case 'Z': fprintf (file, "java_booleanarray*"); break;
277 case 'B': fprintf (file, "java_bytearray*"); break;
278 case 'S': fprintf (file, "java_shortarray*"); break;
279 case 'C': fprintf (file, "java_chararray*"); break;
280 case 'F': fprintf (file, "java_floatarray*"); break;
281 case 'D': fprintf (file, "java_doublearray*"); break;
283 case '[': fprintf (file, "java_objectarray*");
284 while ((c = utf_nextu2(&utf_ptr)) == '[') ;
286 while (utf_nextu2(&utf_ptr) != ';');
289 case 'L': fprintf (file, "java_objectarray*");
290 while ( utf_nextu2(&utf_ptr) != ';');
292 default: panic ("invalid type descriptor");
297 addoutputsize ( sizeof(java_objectheader*));
298 fprintf (file, "struct ");
299 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
303 default: panic ("Unknown type in field descriptor");
310 /***** determine the number of entries of a utf string in the ident chain *****/
312 static int searchidentchain_utf(utf *ident)
314 utf *u = chain_first(ident_chain); /* first element of list */
318 if (u==ident) count++; /* string found */
319 u = chain_next(ident_chain); /* next element in list */
326 /************** print structure for direct access to objects ******************/
328 static void printfields(classinfo *c)
335 addoutputsize(sizeof(java_objectheader));
336 fprintf(file, " java_objectheader header;\n");
340 printfields(c->super.cls);
342 for (i = 0; i < c->fieldscount; i++) {
345 if (!(f->flags & ACC_STATIC)) {
347 printtype(f->descriptor->text);
349 utf_fprint(file, f->name);
351 /* rename multiple fieldnames */
352 if ((ident_count = searchidentchain_utf(f->name)))
353 fprintf(file, "%d", ident_count - 1);
354 chain_addlast(ident_chain, f->name);
356 fprintf(file, ";\n");
362 /***************** store prototype for native method in file ******************/
364 void printmethod(methodinfo *m)
369 /* search for return-type in descriptor */
370 utf_ptr = m->descriptor->text;
371 while (utf_nextu2(&utf_ptr) != ')');
374 fprintf(file, "\n/*\n * Class: ");
375 utf_fprint(file, m->class->name);
376 fprintf(file, "\n * Method: ");
377 utf_fprint(file, m->name);
378 fprintf(file, "\n * Signature: ");
379 utf_fprint(file, m->descriptor);
380 fprintf(file, "\n */\n");
382 /* create prototype */
383 fprintf(file, "JNIEXPORT ");
385 fprintf(file, " JNICALL Java_");
386 printID(m->class->name);
388 chain_addlast(ident_chain, m->name);
392 if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
393 fprintf(file, "(JNIEnv *env");
395 utf_ptr = m->descriptor->text + 1;
397 if (!(m->flags & ACC_STATIC)) {
398 fprintf(file, ", struct ");
399 printID(m->class->name);
400 fprintf(file, "* this");
403 fprintf(file, ", jclass clazz");
406 if ((*utf_ptr) != ')') fprintf(file, ", ");
408 while ((*utf_ptr) != ')') {
409 utf_ptr = printtype(utf_ptr);
410 fprintf(file, " par%d", paramnum++);
411 if ((*utf_ptr)!=')') fprintf(file, ", ");
414 fprintf(file, ");\n\n");
418 /******* remove package-name in fully-qualified classname *********************/
420 void gen_header_filename(char *buffer, utf *u)
424 for (i = 0; i < utf_strlen(u); i++) {
425 if ((u->text[i] == '/') || (u->text[i] == '$')) {
426 buffer[i] = '_'; /* convert '$' and '/' to '_' */
429 buffer[i] = u->text[i];
432 buffer[utf_strlen(u)] = '\0';
436 /* create headerfile for classes and store native methods in chain ************/
438 void headerfile_generate(classinfo *c, char *opt_directory)
440 char header_filename[1024] = "";
441 char classname[1024];
442 char uclassname[1024];
447 u2 nativelyoverloaded;
449 /* store class in chain */
450 chain_addlast(nativeclass_chain, c);
452 /* open headerfile for class */
453 gen_header_filename(classname, c->name);
455 /* create chain for renaming fields */
456 ident_chain = chain_new();
459 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
462 sprintf(header_filename, "%s.h", classname);
465 file = fopen(header_filename, "w");
467 panic("Can not open file to store header information");
469 fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
471 /* convert to uppercase */
472 for (i = 0; classname[i]; i++) {
473 uclassname[i] = toupper(classname[i]);
475 uclassname[i] = '\0';
477 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
479 /* create structure for direct access to objects */
480 fprintf(file, "/* Structure information for class: ");
481 utf_fprint(file, c->name);
482 fprintf(file, " */\n\n");
483 fprintf(file, "typedef struct ");
485 fprintf(file, " {\n");
493 fprintf(file, ";\n\n");
495 /* create chain for renaming overloaded methods */
496 chain_free(ident_chain);
497 ident_chain = chain_new();
499 /* create method-prototypes */
501 /* find overloaded methods */
502 for (i = 0; i < c->methodscount; i++) {
504 m = &(c->methods[i]);
506 if (!(m->flags & ACC_NATIVE)) continue;
507 if (!m->nativelyoverloaded) {
508 nativelyoverloaded=false;
509 for (i2=i+1;i2<c->methodscount; i2++) {
510 m2 = &(c->methods[i2]);
511 if (!(m2->flags & ACC_NATIVE)) continue;
512 if (m->name==m2->name) {
513 m2->nativelyoverloaded=true;
514 nativelyoverloaded=true;
517 m->nativelyoverloaded=nativelyoverloaded;
522 for (i = 0; i < c->methodscount; i++) {
524 m = &(c->methods[i]);
526 if (m->flags & ACC_NATIVE) {
527 chain_addlast(nativemethod_chain, m);
532 chain_free(ident_chain);
534 fprintf(file, "#endif\n\n");
540 /******** print classname, '$' used to seperate inner-class name ***********/
542 void print_classname(classinfo *clazz)
544 utf *u = clazz->name;
545 char *endpos = u->text + u->blength;
546 char *utf_ptr = u->text;
549 while (utf_ptr < endpos) {
550 if ((c = utf_nextu2(&utf_ptr)) == '_') {
561 * These are local overrides for various environment variables in Emacs.
562 * Please do not remove this and leave it at the end of the file, where
563 * Emacs will automagically detect them.
564 * ---------------------------------------------------------------------
567 * indent-tabs-mode: t