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
37 #if defined(ENABLE_THREADS)
38 # if defined(__DARWIN__)
41 # include <ucontext.h>
44 #include "mm/gc-common.h"
45 #include "mm/memory.h"
47 #include "toolbox/chain.h"
48 #include "toolbox/logging.h"
50 #include "vm/builtin.h"
51 #include "vm/global.h"
52 #include "vm/stringlocal.h"
54 #include "vmcore/class.h"
55 #include "vmcore/method.h"
56 #include "vmcore/loader.h"
57 #include "vmcore/options.h"
60 /************************ global variables **********************/
62 #define ACC_NATIVELY_OVERLOADED 0x10000000
64 chain *ident_chain; /* chain with method and field names in current class */
66 static uint32_t outputsize;
67 static bool dopadding;
70 static void printIDpart(int c)
72 if ((c >= 'a' && c <= 'z') ||
73 (c >= 'A' && c <= 'Z') ||
74 (c >= '0' && c <= '9') ||
84 char *utf_ptr = u->text;
87 for (i = 0; i < utf_get_number_of_u2s(u); i++)
88 printIDpart(utf_nextu2(&utf_ptr));
92 static void addoutputsize(int len)
100 newsize = MEMORY_ALIGN(outputsize, len);
102 for (i = outputsize; i < newsize; i++)
103 fprintf(file, " uint8_t 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)) {
152 fprintf(file, "void");
160 fprintf(file, "int32_t");
164 fprintf(file, "int64_t");
168 fprintf(file, "float");
172 fprintf(file, "double");
175 addoutputsize ( sizeof(java_array_t*) );
176 switch (utf_nextu2(&utf_ptr)) {
177 case 'I': fprintf (file, "java_intarray_t*"); break;
178 case 'J': fprintf (file, "java_longarray_t*"); break;
179 case 'Z': fprintf (file, "java_booleanarray_t*"); break;
180 case 'B': fprintf (file, "java_bytearray_t*"); break;
181 case 'S': fprintf (file, "java_shortarray_t*"); break;
182 case 'C': fprintf (file, "java_chararray_t*"); break;
183 case 'F': fprintf (file, "java_floatarray_t*"); break;
184 case 'D': fprintf (file, "java_doublearray_t*"); break;
186 case '[': fprintf(file, "java_objectarray_t*");
187 while ((c = utf_nextu2(&utf_ptr)) == '[');
189 while (utf_nextu2(&utf_ptr) != ';');
192 case 'L': fprintf(file, "java_objectarray_t*");
193 while (utf_nextu2(&utf_ptr) != ';');
196 log_text("invalid type descriptor");
202 addoutputsize ( sizeof(java_object_t*));
203 fprintf (file, "struct ");
204 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);
209 log_text("Unknown type in field descriptor");
217 /***** determine the number of entries of a utf string in the ident chain *****/
219 static int searchidentchain_utf(utf *ident)
221 utf *u = chain_first(ident_chain); /* first element of list */
225 if (u==ident) count++; /* string found */
226 u = chain_next(ident_chain); /* next element in list */
233 /************** print structure for direct access to objects ******************/
235 static void printfields(classinfo *c)
242 addoutputsize(sizeof(java_object_t));
243 fprintf(file, " java_object_t header;\n");
247 printfields(c->super.cls);
249 for (i = 0; i < c->fieldscount; i++) {
252 if (!(f->flags & ACC_STATIC)) {
254 printtype(f->descriptor->text);
256 utf_fprint_printable_ascii(file, f->name);
258 /* rename multiple fieldnames */
259 if ((ident_count = searchidentchain_utf(f->name)))
260 fprintf(file, "%d", ident_count - 1);
261 chain_addlast(ident_chain, f->name);
263 fprintf(file, ";\n");
269 /***************** store prototype for native method in file ******************/
271 void printmethod(methodinfo *m)
274 int32_t paramnum = 1;
276 /* search for return-type in descriptor */
277 utf_ptr = m->descriptor->text;
278 while (utf_nextu2(&utf_ptr) != ')');
281 fprintf(file, "\n/*\n * Class: ");
282 utf_fprint_printable_ascii(file, m->class->name);
283 fprintf(file, "\n * Method: ");
284 utf_fprint_printable_ascii(file, m->name);
285 fprintf(file, "\n * Signature: ");
286 utf_fprint_printable_ascii(file, m->descriptor);
287 fprintf(file, "\n */\n");
289 /* create prototype */
290 fprintf(file, "JNIEXPORT ");
292 fprintf(file, " JNICALL Java_");
293 printID(m->class->name);
295 chain_addlast(ident_chain, m->name);
300 /* ATTENTION: We use a dummy flag here. */
302 if (m->flags & ACC_NATIVELY_OVERLOADED)
303 printOverloadPart(m->descriptor);
305 fprintf(file, "(JNIEnv *env");
307 utf_ptr = m->descriptor->text + 1;
309 if (!(m->flags & ACC_STATIC)) {
310 fprintf(file, ", struct ");
311 printID(m->class->name);
312 fprintf(file, "* this");
315 fprintf(file, ", jclass clazz");
318 if ((*utf_ptr) != ')') fprintf(file, ", ");
320 while ((*utf_ptr) != ')') {
321 utf_ptr = printtype(utf_ptr);
322 fprintf(file, " par%d", paramnum++);
323 if ((*utf_ptr)!=')') fprintf(file, ", ");
326 fprintf(file, ");\n\n");
330 /******* remove package-name in fully-qualified classname *********************/
332 void gen_header_filename(char *buffer, utf *u)
336 for (i = 0; i < utf_get_number_of_u2s(u); i++) {
337 if ((u->text[i] == '/') || (u->text[i] == '$')) {
338 buffer[i] = '_'; /* convert '$' and '/' to '_' */
341 buffer[i] = u->text[i];
344 buffer[utf_get_number_of_u2s(u)] = '\0';
348 /* create headerfile for classes and store native methods in chain ************/
350 void headerfile_generate(classinfo *c, char *opt_directory)
352 char header_filename[1024] = "";
353 char classname[1024];
354 char uclassname[1024];
359 bool nativelyoverloaded;
361 /* prevent compiler warnings */
363 nativelyoverloaded = false;
365 /* open headerfile for class */
366 gen_header_filename(classname, c->name);
368 /* create chain for renaming fields */
369 ident_chain = chain_new();
372 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
375 sprintf(header_filename, "%s.h", classname);
378 file = fopen(header_filename, "w");
380 log_text("Can not open file to store header information");
384 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
386 /* convert to uppercase */
387 for (i = 0; classname[i]; i++) {
388 uclassname[i] = toupper(classname[i]);
390 uclassname[i] = '\0';
392 fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
394 /* create structure for direct access to objects */
395 fprintf(file, "/* Structure information for class: ");
396 utf_fprint_printable_ascii(file, c->name);
397 fprintf(file, " */\n\n");
398 fprintf(file, "typedef struct ");
400 fprintf(file, " {\n");
408 fprintf(file, ";\n\n");
410 /* create chain for renaming overloaded methods */
411 chain_free(ident_chain);
412 ident_chain = chain_new();
414 /* create method-prototypes */
416 /* find overloaded methods */
418 for (i = 0; i < c->methodscount; i++) {
419 m = &(c->methods[i]);
421 if (!(m->flags & ACC_NATIVE))
424 /* We use a dummy flag here. */
426 if (!(m->flags & ACC_NATIVELY_OVERLOADED)) {
427 nativelyoverloaded = false;
429 for (j = i + 1; j < c->methodscount; j++) {
430 m2 = &(c->methods[j]);
432 if (!(m2->flags & ACC_NATIVE))
435 if (m->name == m2->name) {
436 m2->flags |= ACC_NATIVELY_OVERLOADED;
437 nativelyoverloaded = true;
442 if (nativelyoverloaded == true)
443 m->flags |= ACC_NATIVELY_OVERLOADED;
446 for (i = 0; i < c->methodscount; i++) {
447 m = &(c->methods[i]);
449 if (m->flags & ACC_NATIVE)
453 chain_free(ident_chain);
455 fprintf(file, "#endif\n\n");
461 /******** print classname, '$' used to seperate inner-class name ***********/
463 void print_classname(classinfo *clazz)
465 utf *u = clazz->name;
466 char *endpos = u->text + u->blength;
467 char *utf_ptr = u->text;
470 while (utf_ptr < endpos) {
471 if ((c = utf_nextu2(&utf_ptr)) == '_')
480 * These are local overrides for various environment variables in Emacs.
481 * Please do not remove this and leave it at the end of the file, where
482 * Emacs will automagically detect them.
483 * ---------------------------------------------------------------------
486 * indent-tabs-mode: t
490 * vim:noexpandtab:sw=4:ts=4: