1 /* src/cacaoh/headers.c - functions for header generation
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
25 $Id: headers.c 7486 2007-03-08 13:50:07Z twisti $
40 #if defined(ENABLE_THREADS)
41 # if defined(__DARWIN__)
44 # include <ucontext.h>
47 #include "mm/gc-common.h"
48 #include "mm/memory.h"
50 #include "toolbox/chain.h"
51 #include "toolbox/logging.h"
53 #include "vm/builtin.h"
54 #include "vm/global.h"
55 #include "vm/stringlocal.h"
57 #include "vmcore/class.h"
58 #include "vmcore/method.h"
59 #include "vmcore/loader.h"
60 #include "vmcore/options.h"
63 /************************ global variables **********************/
65 #define ACC_NATIVELY_OVERLOADED 0x10000000
67 chain *ident_chain; /* chain with method and field names in current class */
70 static bool dopadding;
73 static void printIDpart(int c)
75 if ((c >= 'a' && c <= 'z') ||
76 (c >= 'A' && c <= 'Z') ||
77 (c >= '0' && c <= '9') ||
87 char *utf_ptr = u->text;
90 for (i = 0; i < utf_get_number_of_u2s(u); i++)
91 printIDpart(utf_nextu2(&utf_ptr));
95 static void addoutputsize (int len)
98 if (!dopadding) return;
100 newsize = MEMORY_ALIGN(outputsize, len);
102 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
103 outputsize = newsize;
107 void printOverloadPart(utf *desc)
109 char *utf_ptr=desc->text;
114 while ((c = utf_nextu2(&utf_ptr)) != ')') {
124 fprintf(file, "%c", (char) c);
131 while ((c = utf_nextu2(&utf_ptr)) != ';')
138 log_text("invalid method descriptor");
144 static char *printtype(char *utf_ptr)
148 switch (utf_nextu2(&utf_ptr)) {
149 case 'V': fprintf (file, "void");
155 case 'Z': addoutputsize (4);
156 fprintf (file, "s4");
158 case 'J': addoutputsize (8);
159 fprintf (file, "s8");
161 case 'F': addoutputsize (4);
162 fprintf (file, "float");
164 case 'D': addoutputsize (8);
165 fprintf (file, "double");
168 addoutputsize ( sizeof(java_arrayheader*) );
169 switch (utf_nextu2(&utf_ptr)) {
170 case 'I': fprintf (file, "java_intarray*"); break;
171 case 'J': fprintf (file, "java_longarray*"); break;
172 case 'Z': fprintf (file, "java_booleanarray*"); break;
173 case 'B': fprintf (file, "java_bytearray*"); break;
174 case 'S': fprintf (file, "java_shortarray*"); break;
175 case 'C': fprintf (file, "java_chararray*"); break;
176 case 'F': fprintf (file, "java_floatarray*"); break;
177 case 'D': fprintf (file, "java_doublearray*"); break;
179 case '[': fprintf(file, "java_objectarray*");
180 while ((c = utf_nextu2(&utf_ptr)) == '[');
182 while (utf_nextu2(&utf_ptr) != ';');
185 case 'L': fprintf(file, "java_objectarray*");
186 while (utf_nextu2(&utf_ptr) != ';');
189 log_text("invalid type descriptor");
195 addoutputsize ( sizeof(java_objectheader*));
196 fprintf (file, "struct ");
197 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
202 log_text("Unknown type in field descriptor");
210 /***** determine the number of entries of a utf string in the ident chain *****/
212 static int searchidentchain_utf(utf *ident)
214 utf *u = chain_first(ident_chain); /* first element of list */
218 if (u==ident) count++; /* string found */
219 u = chain_next(ident_chain); /* next element in list */
226 /************** print structure for direct access to objects ******************/
228 static void printfields(classinfo *c)
235 addoutputsize(sizeof(java_objectheader));
236 fprintf(file, " java_objectheader header;\n");
240 printfields(c->super.cls);
242 for (i = 0; i < c->fieldscount; i++) {
245 if (!(f->flags & ACC_STATIC)) {
247 printtype(f->descriptor->text);
249 utf_fprint_printable_ascii(file, f->name);
251 /* rename multiple fieldnames */
252 if ((ident_count = searchidentchain_utf(f->name)))
253 fprintf(file, "%d", ident_count - 1);
254 chain_addlast(ident_chain, f->name);
256 fprintf(file, ";\n");
262 /***************** store prototype for native method in file ******************/
264 void printmethod(methodinfo *m)
269 /* search for return-type in descriptor */
270 utf_ptr = m->descriptor->text;
271 while (utf_nextu2(&utf_ptr) != ')');
274 fprintf(file, "\n/*\n * Class: ");
275 utf_fprint_printable_ascii(file, m->class->name);
276 fprintf(file, "\n * Method: ");
277 utf_fprint_printable_ascii(file, m->name);
278 fprintf(file, "\n * Signature: ");
279 utf_fprint_printable_ascii(file, m->descriptor);
280 fprintf(file, "\n */\n");
282 /* create prototype */
283 fprintf(file, "JNIEXPORT ");
285 fprintf(file, " JNICALL Java_");
286 printID(m->class->name);
288 chain_addlast(ident_chain, m->name);
293 /* ATTENTION: We use a dummy flag here. */
295 if (m->flags & ACC_NATIVELY_OVERLOADED)
296 printOverloadPart(m->descriptor);
298 fprintf(file, "(JNIEnv *env");
300 utf_ptr = m->descriptor->text + 1;
302 if (!(m->flags & ACC_STATIC)) {
303 fprintf(file, ", struct ");
304 printID(m->class->name);
305 fprintf(file, "* this");
308 fprintf(file, ", jclass clazz");
311 if ((*utf_ptr) != ')') fprintf(file, ", ");
313 while ((*utf_ptr) != ')') {
314 utf_ptr = printtype(utf_ptr);
315 fprintf(file, " par%d", paramnum++);
316 if ((*utf_ptr)!=')') fprintf(file, ", ");
319 fprintf(file, ");\n\n");
323 /******* remove package-name in fully-qualified classname *********************/
325 void gen_header_filename(char *buffer, utf *u)
329 for (i = 0; i < utf_get_number_of_u2s(u); i++) {
330 if ((u->text[i] == '/') || (u->text[i] == '$')) {
331 buffer[i] = '_'; /* convert '$' and '/' to '_' */
334 buffer[i] = u->text[i];
337 buffer[utf_get_number_of_u2s(u)] = '\0';
341 /* create headerfile for classes and store native methods in chain ************/
343 void headerfile_generate(classinfo *c, char *opt_directory)
345 char header_filename[1024] = "";
346 char classname[1024];
347 char uclassname[1024];
352 bool nativelyoverloaded;
354 /* prevent compiler warnings */
356 nativelyoverloaded = false;
358 /* open headerfile for class */
359 gen_header_filename(classname, c->name);
361 /* create chain for renaming fields */
362 ident_chain = chain_new();
365 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
368 sprintf(header_filename, "%s.h", classname);
371 file = fopen(header_filename, "w");
373 log_text("Can not open file to store header information");
377 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
379 /* convert to uppercase */
380 for (i = 0; classname[i]; i++) {
381 uclassname[i] = toupper(classname[i]);
383 uclassname[i] = '\0';
385 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
387 /* create structure for direct access to objects */
388 fprintf(file, "/* Structure information for class: ");
389 utf_fprint_printable_ascii(file, c->name);
390 fprintf(file, " */\n\n");
391 fprintf(file, "typedef struct ");
393 fprintf(file, " {\n");
401 fprintf(file, ";\n\n");
403 /* create chain for renaming overloaded methods */
404 chain_free(ident_chain);
405 ident_chain = chain_new();
407 /* create method-prototypes */
409 /* find overloaded methods */
411 for (i = 0; i < c->methodscount; i++) {
412 m = &(c->methods[i]);
414 if (!(m->flags & ACC_NATIVE))
417 /* We use a dummy flag here. */
419 if (!(m->flags & ACC_NATIVELY_OVERLOADED)) {
420 nativelyoverloaded = false;
422 for (j = i + 1; j < c->methodscount; j++) {
423 m2 = &(c->methods[j]);
425 if (!(m2->flags & ACC_NATIVE))
428 if (m->name == m2->name) {
429 m2->flags |= ACC_NATIVELY_OVERLOADED;
430 nativelyoverloaded = true;
435 if (nativelyoverloaded == true)
436 m->flags |= ACC_NATIVELY_OVERLOADED;
439 for (i = 0; i < c->methodscount; i++) {
440 m = &(c->methods[i]);
442 if (m->flags & ACC_NATIVE)
446 chain_free(ident_chain);
448 fprintf(file, "#endif\n\n");
454 /******** print classname, '$' used to seperate inner-class name ***********/
456 void print_classname(classinfo *clazz)
458 utf *u = clazz->name;
459 char *endpos = u->text + u->blength;
460 char *utf_ptr = u->text;
463 while (utf_ptr < endpos) {
464 if ((c = utf_nextu2(&utf_ptr)) == '_')
473 * These are local overrides for various environment variables in Emacs.
474 * Please do not remove this and leave it at the end of the file, where
475 * Emacs will automagically detect them.
476 * ---------------------------------------------------------------------
479 * indent-tabs-mode: t
483 * vim:noexpandtab:sw=4:ts=4: