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 7246 2007-01-29 18:49:05Z 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 "vm/jit/asmpart.h"
59 #include "vmcore/class.h"
60 #include "vmcore/method.h"
61 #include "vmcore/loader.h"
62 #include "vmcore/options.h"
65 /************************ global variables **********************/
67 #define ACC_NATIVELY_OVERLOADED 0x10000000
69 chain *ident_chain; /* chain with method and field names in current class */
72 static bool dopadding;
75 static void printIDpart(int c)
77 if ((c >= 'a' && c <= 'z') ||
78 (c >= 'A' && c <= 'Z') ||
79 (c >= '0' && c <= '9') ||
89 char *utf_ptr = u->text;
92 for (i = 0; i < utf_get_number_of_u2s(u); i++)
93 printIDpart(utf_nextu2(&utf_ptr));
97 static void addoutputsize (int len)
100 if (!dopadding) return;
102 newsize = MEMORY_ALIGN(outputsize, len);
104 for (i = outputsize; i < newsize; i++) fprintf(file, " u1 pad%d\n", (int) i);
105 outputsize = newsize;
109 void printOverloadPart(utf *desc)
111 char *utf_ptr=desc->text;
116 while ((c = utf_nextu2(&utf_ptr)) != ')') {
126 fprintf(file, "%c", (char) c);
133 while ((c = utf_nextu2(&utf_ptr)) != ';')
140 log_text("invalid method descriptor");
146 static char *printtype(char *utf_ptr)
150 switch (utf_nextu2(&utf_ptr)) {
151 case 'V': fprintf (file, "void");
157 case 'Z': addoutputsize (4);
158 fprintf (file, "s4");
160 case 'J': addoutputsize (8);
161 fprintf (file, "s8");
163 case 'F': addoutputsize (4);
164 fprintf (file, "float");
166 case 'D': addoutputsize (8);
167 fprintf (file, "double");
170 addoutputsize ( sizeof(java_arrayheader*) );
171 switch (utf_nextu2(&utf_ptr)) {
172 case 'I': fprintf (file, "java_intarray*"); break;
173 case 'J': fprintf (file, "java_longarray*"); break;
174 case 'Z': fprintf (file, "java_booleanarray*"); break;
175 case 'B': fprintf (file, "java_bytearray*"); break;
176 case 'S': fprintf (file, "java_shortarray*"); break;
177 case 'C': fprintf (file, "java_chararray*"); break;
178 case 'F': fprintf (file, "java_floatarray*"); break;
179 case 'D': fprintf (file, "java_doublearray*"); break;
181 case '[': fprintf(file, "java_objectarray*");
182 while ((c = utf_nextu2(&utf_ptr)) == '[');
184 while (utf_nextu2(&utf_ptr) != ';');
187 case 'L': fprintf(file, "java_objectarray*");
188 while (utf_nextu2(&utf_ptr) != ';');
191 log_text("invalid type descriptor");
197 addoutputsize ( sizeof(java_objectheader*));
198 fprintf (file, "struct ");
199 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
204 log_text("Unknown type in field descriptor");
212 /***** determine the number of entries of a utf string in the ident chain *****/
214 static int searchidentchain_utf(utf *ident)
216 utf *u = chain_first(ident_chain); /* first element of list */
220 if (u==ident) count++; /* string found */
221 u = chain_next(ident_chain); /* next element in list */
228 /************** print structure for direct access to objects ******************/
230 static void printfields(classinfo *c)
237 addoutputsize(sizeof(java_objectheader));
238 fprintf(file, " java_objectheader header;\n");
242 printfields(c->super.cls);
244 for (i = 0; i < c->fieldscount; i++) {
247 if (!(f->flags & ACC_STATIC)) {
249 printtype(f->descriptor->text);
251 utf_fprint_printable_ascii(file, f->name);
253 /* rename multiple fieldnames */
254 if ((ident_count = searchidentchain_utf(f->name)))
255 fprintf(file, "%d", ident_count - 1);
256 chain_addlast(ident_chain, f->name);
258 fprintf(file, ";\n");
264 /***************** store prototype for native method in file ******************/
266 void printmethod(methodinfo *m)
271 /* search for return-type in descriptor */
272 utf_ptr = m->descriptor->text;
273 while (utf_nextu2(&utf_ptr) != ')');
276 fprintf(file, "\n/*\n * Class: ");
277 utf_fprint_printable_ascii(file, m->class->name);
278 fprintf(file, "\n * Method: ");
279 utf_fprint_printable_ascii(file, m->name);
280 fprintf(file, "\n * Signature: ");
281 utf_fprint_printable_ascii(file, m->descriptor);
282 fprintf(file, "\n */\n");
284 /* create prototype */
285 fprintf(file, "JNIEXPORT ");
287 fprintf(file, " JNICALL Java_");
288 printID(m->class->name);
290 chain_addlast(ident_chain, m->name);
295 /* ATTENTION: We use a dummy flag here. */
297 if (m->flags & ACC_NATIVELY_OVERLOADED)
298 printOverloadPart(m->descriptor);
300 fprintf(file, "(JNIEnv *env");
302 utf_ptr = m->descriptor->text + 1;
304 if (!(m->flags & ACC_STATIC)) {
305 fprintf(file, ", struct ");
306 printID(m->class->name);
307 fprintf(file, "* this");
310 fprintf(file, ", jclass clazz");
313 if ((*utf_ptr) != ')') fprintf(file, ", ");
315 while ((*utf_ptr) != ')') {
316 utf_ptr = printtype(utf_ptr);
317 fprintf(file, " par%d", paramnum++);
318 if ((*utf_ptr)!=')') fprintf(file, ", ");
321 fprintf(file, ");\n\n");
325 /******* remove package-name in fully-qualified classname *********************/
327 void gen_header_filename(char *buffer, utf *u)
331 for (i = 0; i < utf_get_number_of_u2s(u); i++) {
332 if ((u->text[i] == '/') || (u->text[i] == '$')) {
333 buffer[i] = '_'; /* convert '$' and '/' to '_' */
336 buffer[i] = u->text[i];
339 buffer[utf_get_number_of_u2s(u)] = '\0';
343 /* create headerfile for classes and store native methods in chain ************/
345 void headerfile_generate(classinfo *c, char *opt_directory)
347 char header_filename[1024] = "";
348 char classname[1024];
349 char uclassname[1024];
354 bool nativelyoverloaded;
356 /* prevent compiler warnings */
358 nativelyoverloaded = false;
360 /* open headerfile for class */
361 gen_header_filename(classname, c->name);
363 /* create chain for renaming fields */
364 ident_chain = chain_new();
367 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
370 sprintf(header_filename, "%s.h", classname);
373 file = fopen(header_filename, "w");
375 log_text("Can not open file to store header information");
379 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
381 /* convert to uppercase */
382 for (i = 0; classname[i]; i++) {
383 uclassname[i] = toupper(classname[i]);
385 uclassname[i] = '\0';
387 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
389 /* create structure for direct access to objects */
390 fprintf(file, "/* Structure information for class: ");
391 utf_fprint_printable_ascii(file, c->name);
392 fprintf(file, " */\n\n");
393 fprintf(file, "typedef struct ");
395 fprintf(file, " {\n");
403 fprintf(file, ";\n\n");
405 /* create chain for renaming overloaded methods */
406 chain_free(ident_chain);
407 ident_chain = chain_new();
409 /* create method-prototypes */
411 /* find overloaded methods */
413 for (i = 0; i < c->methodscount; i++) {
414 m = &(c->methods[i]);
416 if (!(m->flags & ACC_NATIVE))
419 /* We use a dummy flag here. */
421 if (!(m->flags & ACC_NATIVELY_OVERLOADED)) {
422 nativelyoverloaded = false;
424 for (j = i + 1; j < c->methodscount; j++) {
425 m2 = &(c->methods[j]);
427 if (!(m2->flags & ACC_NATIVE))
430 if (m->name == m2->name) {
431 m2->flags |= ACC_NATIVELY_OVERLOADED;
432 nativelyoverloaded = true;
437 if (nativelyoverloaded == true)
438 m->flags |= ACC_NATIVELY_OVERLOADED;
441 for (i = 0; i < c->methodscount; i++) {
442 m = &(c->methods[i]);
444 if (m->flags & ACC_NATIVE)
448 chain_free(ident_chain);
450 fprintf(file, "#endif\n\n");
456 /******** print classname, '$' used to seperate inner-class name ***********/
458 void print_classname(classinfo *clazz)
460 utf *u = clazz->name;
461 char *endpos = u->text + u->blength;
462 char *utf_ptr = u->text;
465 while (utf_ptr < endpos) {
466 if ((c = utf_nextu2(&utf_ptr)) == '_')
475 * These are local overrides for various environment variables in Emacs.
476 * Please do not remove this and leave it at the end of the file, where
477 * Emacs will automagically detect them.
478 * ---------------------------------------------------------------------
481 * indent-tabs-mode: t
485 * vim:noexpandtab:sw=4:ts=4: