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 8318 2007-08-16 10:05:34Z michi $
39 #if defined(ENABLE_THREADS)
40 # if defined(__DARWIN__)
43 # include <ucontext.h>
46 #include "mm/gc-common.h"
47 #include "mm/memory.h"
49 #include "toolbox/chain.h"
50 #include "toolbox/logging.h"
52 #include "vm/builtin.h"
53 #include "vm/global.h"
54 #include "vm/stringlocal.h"
56 #include "vmcore/class.h"
57 #include "vmcore/method.h"
58 #include "vmcore/loader.h"
59 #include "vmcore/options.h"
62 /************************ global variables **********************/
64 #define ACC_NATIVELY_OVERLOADED 0x10000000
66 chain *ident_chain; /* chain with method and field names in current class */
68 static uint32_t outputsize;
69 static bool dopadding;
72 static void printIDpart(int c)
74 if ((c >= 'a' && c <= 'z') ||
75 (c >= 'A' && c <= 'Z') ||
76 (c >= '0' && c <= '9') ||
86 char *utf_ptr = u->text;
89 for (i = 0; i < utf_get_number_of_u2s(u); i++)
90 printIDpart(utf_nextu2(&utf_ptr));
94 static void addoutputsize(int len)
102 newsize = MEMORY_ALIGN(outputsize, len);
104 for (i = outputsize; i < newsize; i++)
105 fprintf(file, " uint8_t pad%d\n", (int) i);
107 outputsize = newsize;
111 void printOverloadPart(utf *desc)
113 char *utf_ptr=desc->text;
118 while ((c = utf_nextu2(&utf_ptr)) != ')') {
128 fprintf(file, "%c", (char) c);
135 while ((c = utf_nextu2(&utf_ptr)) != ';')
142 log_text("invalid method descriptor");
148 static char *printtype(char *utf_ptr)
152 switch (utf_nextu2(&utf_ptr)) {
154 fprintf(file, "void");
162 fprintf(file, "int32_t");
166 fprintf(file, "int64_t");
170 fprintf(file, "float");
174 fprintf(file, "double");
177 addoutputsize ( sizeof(java_array_t*) );
178 switch (utf_nextu2(&utf_ptr)) {
179 case 'I': fprintf (file, "java_intarray_t*"); break;
180 case 'J': fprintf (file, "java_longarray_t*"); break;
181 case 'Z': fprintf (file, "java_booleanarray_t*"); break;
182 case 'B': fprintf (file, "java_bytearray_t*"); break;
183 case 'S': fprintf (file, "java_shortarray_t*"); break;
184 case 'C': fprintf (file, "java_chararray_t*"); break;
185 case 'F': fprintf (file, "java_floatarray_t*"); break;
186 case 'D': fprintf (file, "java_doublearray_t*"); break;
188 case '[': fprintf(file, "java_objectarray_t*");
189 while ((c = utf_nextu2(&utf_ptr)) == '[');
191 while (utf_nextu2(&utf_ptr) != ';');
194 case 'L': fprintf(file, "java_objectarray_t*");
195 while (utf_nextu2(&utf_ptr) != ';');
198 log_text("invalid type descriptor");
204 addoutputsize ( sizeof(java_object_t*));
205 fprintf (file, "struct ");
206 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
211 log_text("Unknown type in field descriptor");
219 /***** determine the number of entries of a utf string in the ident chain *****/
221 static int searchidentchain_utf(utf *ident)
223 utf *u = chain_first(ident_chain); /* first element of list */
227 if (u==ident) count++; /* string found */
228 u = chain_next(ident_chain); /* next element in list */
235 /************** print structure for direct access to objects ******************/
237 static void printfields(classinfo *c)
244 addoutputsize(sizeof(java_object_t));
245 fprintf(file, " java_object_t header;\n");
249 printfields(c->super.cls);
251 for (i = 0; i < c->fieldscount; i++) {
254 if (!(f->flags & ACC_STATIC)) {
256 printtype(f->descriptor->text);
258 utf_fprint_printable_ascii(file, f->name);
260 /* rename multiple fieldnames */
261 if ((ident_count = searchidentchain_utf(f->name)))
262 fprintf(file, "%d", ident_count - 1);
263 chain_addlast(ident_chain, f->name);
265 fprintf(file, ";\n");
271 /***************** store prototype for native method in file ******************/
273 void printmethod(methodinfo *m)
276 int32_t paramnum = 1;
278 /* search for return-type in descriptor */
279 utf_ptr = m->descriptor->text;
280 while (utf_nextu2(&utf_ptr) != ')');
283 fprintf(file, "\n/*\n * Class: ");
284 utf_fprint_printable_ascii(file, m->class->name);
285 fprintf(file, "\n * Method: ");
286 utf_fprint_printable_ascii(file, m->name);
287 fprintf(file, "\n * Signature: ");
288 utf_fprint_printable_ascii(file, m->descriptor);
289 fprintf(file, "\n */\n");
291 /* create prototype */
292 fprintf(file, "JNIEXPORT ");
294 fprintf(file, " JNICALL Java_");
295 printID(m->class->name);
297 chain_addlast(ident_chain, m->name);
302 /* ATTENTION: We use a dummy flag here. */
304 if (m->flags & ACC_NATIVELY_OVERLOADED)
305 printOverloadPart(m->descriptor);
307 fprintf(file, "(JNIEnv *env");
309 utf_ptr = m->descriptor->text + 1;
311 if (!(m->flags & ACC_STATIC)) {
312 fprintf(file, ", struct ");
313 printID(m->class->name);
314 fprintf(file, "* this");
317 fprintf(file, ", jclass clazz");
320 if ((*utf_ptr) != ')') fprintf(file, ", ");
322 while ((*utf_ptr) != ')') {
323 utf_ptr = printtype(utf_ptr);
324 fprintf(file, " par%d", paramnum++);
325 if ((*utf_ptr)!=')') fprintf(file, ", ");
328 fprintf(file, ");\n\n");
332 /******* remove package-name in fully-qualified classname *********************/
334 void gen_header_filename(char *buffer, utf *u)
338 for (i = 0; i < utf_get_number_of_u2s(u); i++) {
339 if ((u->text[i] == '/') || (u->text[i] == '$')) {
340 buffer[i] = '_'; /* convert '$' and '/' to '_' */
343 buffer[i] = u->text[i];
346 buffer[utf_get_number_of_u2s(u)] = '\0';
350 /* create headerfile for classes and store native methods in chain ************/
352 void headerfile_generate(classinfo *c, char *opt_directory)
354 char header_filename[1024] = "";
355 char classname[1024];
356 char uclassname[1024];
361 bool nativelyoverloaded;
363 /* prevent compiler warnings */
365 nativelyoverloaded = false;
367 /* open headerfile for class */
368 gen_header_filename(classname, c->name);
370 /* create chain for renaming fields */
371 ident_chain = chain_new();
374 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
377 sprintf(header_filename, "%s.h", classname);
380 file = fopen(header_filename, "w");
382 log_text("Can not open file to store header information");
386 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
388 /* convert to uppercase */
389 for (i = 0; classname[i]; i++) {
390 uclassname[i] = toupper(classname[i]);
392 uclassname[i] = '\0';
394 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
396 /* create structure for direct access to objects */
397 fprintf(file, "/* Structure information for class: ");
398 utf_fprint_printable_ascii(file, c->name);
399 fprintf(file, " */\n\n");
400 fprintf(file, "typedef struct ");
402 fprintf(file, " {\n");
410 fprintf(file, ";\n\n");
412 /* create chain for renaming overloaded methods */
413 chain_free(ident_chain);
414 ident_chain = chain_new();
416 /* create method-prototypes */
418 /* find overloaded methods */
420 for (i = 0; i < c->methodscount; i++) {
421 m = &(c->methods[i]);
423 if (!(m->flags & ACC_NATIVE))
426 /* We use a dummy flag here. */
428 if (!(m->flags & ACC_NATIVELY_OVERLOADED)) {
429 nativelyoverloaded = false;
431 for (j = i + 1; j < c->methodscount; j++) {
432 m2 = &(c->methods[j]);
434 if (!(m2->flags & ACC_NATIVE))
437 if (m->name == m2->name) {
438 m2->flags |= ACC_NATIVELY_OVERLOADED;
439 nativelyoverloaded = true;
444 if (nativelyoverloaded == true)
445 m->flags |= ACC_NATIVELY_OVERLOADED;
448 for (i = 0; i < c->methodscount; i++) {
449 m = &(c->methods[i]);
451 if (m->flags & ACC_NATIVE)
455 chain_free(ident_chain);
457 fprintf(file, "#endif\n\n");
463 /******** print classname, '$' used to seperate inner-class name ***********/
465 void print_classname(classinfo *clazz)
467 utf *u = clazz->name;
468 char *endpos = u->text + u->blength;
469 char *utf_ptr = u->text;
472 while (utf_ptr < endpos) {
473 if ((c = utf_nextu2(&utf_ptr)) == '_')
482 * These are local overrides for various environment variables in Emacs.
483 * Please do not remove this and leave it at the end of the file, where
484 * Emacs will automagically detect them.
485 * ---------------------------------------------------------------------
488 * indent-tabs-mode: t
492 * vim:noexpandtab:sw=4:ts=4: