* Merged with default branch at rev 16f3633aaa5a.
[cacao.git] / src / cacaoh / headers.c
1 /* src/cacaoh/headers.c - functions for header generation
2
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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <ctype.h>
32 #include <stdarg.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #if defined(ENABLE_THREADS)
38 # if defined(__DARWIN__)
39 #  include <signal.h>
40 # endif
41 # include <ucontext.h>
42 #endif
43
44 #include "mm/gc-common.h"
45 #include "mm/memory.h"
46
47 #include "toolbox/chain.h"
48 #include "toolbox/logging.h"
49
50 #include "vm/builtin.h"
51 #include "vm/global.h"
52 #include "vm/stringlocal.h"
53
54 #include "vmcore/class.h"
55 #include "vmcore/method.h"
56 #include "vmcore/loader.h"
57 #include "vmcore/options.h"
58
59
60 /************************ global variables **********************/
61
62 #define ACC_NATIVELY_OVERLOADED    0x10000000
63
64 #if defined(ENABLE_HANDLES)
65 # define HEAP_PREFIX "heap_"
66 #else
67 # define HEAP_PREFIX ""
68 #endif
69
70 chain *ident_chain;     /* chain with method and field names in current class */
71 FILE *file = NULL;
72 static uint32_t outputsize;
73 static bool dopadding;
74
75
76 static void printIDpart(int c)
77 {
78         if ((c >= 'a' && c <= 'z') ||
79                 (c >= 'A' && c <= 'Z') ||
80                 (c >= '0' && c <= '9') ||
81                 (c == '_'))
82                 putc(c, file);
83         else
84                 putc('_', file);
85 }
86
87
88 void printID(utf *u)
89 {
90         char *utf_ptr = u->text;
91         int i;
92
93         for (i = 0; i < utf_get_number_of_u2s(u); i++) 
94                 printIDpart(utf_nextu2(&utf_ptr));
95 }
96
97
98 static void addoutputsize(int len)
99 {
100         uint32_t newsize;
101         int32_t  i;
102
103         if (!dopadding)
104                 return;
105
106         newsize = MEMORY_ALIGN(outputsize, len);
107         
108         for (i = outputsize; i < newsize; i++)
109                 fprintf(file, "   uint8_t pad%d\n", (int) i);
110
111         outputsize = newsize;
112 }
113
114
115 void printOverloadPart(utf *desc)
116 {
117         char *utf_ptr=desc->text;
118         uint16_t c;
119
120         fprintf(file, "__");
121
122         while ((c = utf_nextu2(&utf_ptr)) != ')') {
123                 switch (c) {
124                 case 'I':
125                 case 'S':
126                 case 'B':
127                 case 'C':
128                 case 'Z':
129                 case 'J':
130                 case 'F':
131                 case 'D': 
132                         fprintf(file, "%c", (char) c);
133                         break;
134                 case '[':
135                         fprintf(file, "_3");
136                         break;
137                 case 'L':
138                         putc('L', file);
139                         while ((c = utf_nextu2(&utf_ptr)) != ';')
140                                 printIDpart(c);
141                         fprintf(file, "_2");
142                         break;
143                 case '(':
144                         break;
145                 default: 
146                         log_text("invalid method descriptor");
147                         assert(0);
148                 }
149         }
150 }
151
152 static char *printtype(char *utf_ptr, char *prefix, char *infix)
153 {
154         uint16_t c;
155
156         switch (utf_nextu2(&utf_ptr)) {
157         case 'V':
158                 fprintf(file, "void");
159                 break;
160         case 'I':
161         case 'S':
162         case 'B':
163         case 'C':
164         case 'Z':
165                 addoutputsize(4);
166                 fprintf(file, "int32_t");
167                 break;
168         case 'J':
169                 addoutputsize(8);
170                 fprintf(file, "int64_t");
171                 break;
172         case 'F':
173                 addoutputsize(4);
174                 fprintf(file, "float");
175                 break;
176         case 'D':
177                 addoutputsize(8);
178                 fprintf(file, "double");
179                 break;
180         case '[':
181                 addoutputsize ( sizeof(java_array_t*) ); 
182                 switch (utf_nextu2(&utf_ptr)) {
183                 case 'I':  fprintf (file, "java%s_intarray_t*", infix); break;
184                 case 'J':  fprintf (file, "java%s_longarray_t*", infix); break;
185                 case 'Z':  fprintf (file, "java%s_booleanarray_t*", infix); break;
186                 case 'B':  fprintf (file, "java%s_bytearray_t*", infix); break;
187                 case 'S':  fprintf (file, "java%s_shortarray_t*", infix); break;
188                 case 'C':  fprintf (file, "java%s_chararray_t*", infix); break;
189                 case 'F':  fprintf (file, "java%s_floatarray_t*", infix); break;
190                 case 'D':  fprintf (file, "java%s_doublearray_t*", infix); break;
191                                 
192                 case '[': fprintf(file, "java%s_objectarray_t*", infix);
193                         while ((c = utf_nextu2(&utf_ptr)) == '[');
194                         if (c == 'L')
195                                 while (utf_nextu2(&utf_ptr) != ';');
196                         break;
197                            
198                 case 'L':  fprintf(file, "java%s_objectarray_t*", infix);
199                         while (utf_nextu2(&utf_ptr) != ';');
200                         break;
201                 default:
202                         log_text("invalid type descriptor");
203                         assert(0);
204                 }
205                 break;
206                 
207         case 'L': 
208                 addoutputsize ( sizeof(java_object_t*));
209                 fprintf (file, "struct %s", prefix);
210                 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);     
211                 fprintf (file, "*");
212                 break;
213                                         
214         default:
215                 log_text("Unknown type in field descriptor");
216                 assert(0);
217         }
218         
219         return utf_ptr;
220 }
221
222
223 /***** determine the number of entries of a utf string in the ident chain *****/
224
225 static int searchidentchain_utf(utf *ident) 
226 {
227         utf *u = chain_first(ident_chain);     /* first element of list */
228         int count = 0;
229
230         while (u) {
231                 if (u==ident) count++;         /* string found */
232                 u = chain_next(ident_chain);   /* next element in list */ 
233         }
234
235         return count;
236 }
237
238
239 /************** print structure for direct access to objects ******************/
240
241 static void printfields(classinfo *c)
242 {
243         int32_t i;
244         fieldinfo *f;
245         int ident_count;
246         
247         if (!c) {
248                 addoutputsize(sizeof(java_object_t));
249                 fprintf(file, "   java_object_t header;\n");
250                 return;
251         }
252                 
253         printfields(c->super.cls);
254         
255         for (i = 0; i < c->fieldscount; i++) {
256                 f = &(c->fields[i]);
257                 
258                 if (!(f->flags & ACC_STATIC)) {
259                         fprintf(file, "   ");
260                         printtype(f->descriptor->text, HEAP_PREFIX, "");
261                         fprintf(file, " ");
262                         utf_fprint_printable_ascii(file, f->name);
263
264                         /* rename multiple fieldnames */
265                         if ((ident_count = searchidentchain_utf(f->name)))
266                                 fprintf(file, "%d", ident_count - 1);
267                         chain_addlast(ident_chain, f->name);    
268
269                         fprintf(file, ";\n");
270                 }
271         }
272 }
273
274
275 /***************** store prototype for native method in file ******************/
276
277 void printmethod(methodinfo *m)
278 {
279         char *utf_ptr;
280         int32_t paramnum = 1;
281
282         /* search for return-type in descriptor */      
283         utf_ptr = m->descriptor->text;
284         while (utf_nextu2(&utf_ptr) != ')');
285
286         /* create remarks */
287         fprintf(file, "\n/*\n * Class:     ");
288         utf_fprint_printable_ascii(file, m->class->name);
289         fprintf(file, "\n * Method:    ");
290         utf_fprint_printable_ascii(file, m->name);
291         fprintf(file, "\n * Signature: ");
292         utf_fprint_printable_ascii(file, m->descriptor);
293         fprintf(file, "\n */\n");
294
295         /* create prototype */                  
296         fprintf(file, "JNIEXPORT ");
297         printtype(utf_ptr, "", "_handle");
298         fprintf(file, " JNICALL Java_");
299         printID(m->class->name);
300
301         chain_addlast(ident_chain, m->name);
302
303         fprintf(file, "_");
304         printID(m->name);
305
306         /* ATTENTION: We use a dummy flag here. */
307
308         if (m->flags & ACC_NATIVELY_OVERLOADED)
309                 printOverloadPart(m->descriptor);
310
311         fprintf(file, "(JNIEnv *env");
312         
313         utf_ptr = m->descriptor->text + 1;
314                         
315         if (!(m->flags & ACC_STATIC)) {
316                 fprintf(file, ", struct ");
317                 printID(m->class->name);
318                 fprintf(file, "* this");
319
320         } else {
321                 fprintf(file, ", jclass clazz");
322         }
323
324         if ((*utf_ptr) != ')') fprintf(file, ", ");
325                         
326         while ((*utf_ptr) != ')') {
327                 utf_ptr = printtype(utf_ptr, "", "_handle");
328                 fprintf(file, " par%d", paramnum++);
329                 if ((*utf_ptr)!=')') fprintf(file, ", ");
330         }
331                         
332         fprintf(file, ");\n\n");
333 }
334
335
336 /******* remove package-name in fully-qualified classname *********************/
337
338 void gen_header_filename(char *buffer, utf *u)
339 {
340         int32_t i;
341   
342         for (i = 0; i < utf_get_number_of_u2s(u); i++) {
343                 if ((u->text[i] == '/') || (u->text[i] == '$')) {
344                         buffer[i] = '_';  /* convert '$' and '/' to '_' */
345
346                 } else {
347                         buffer[i] = u->text[i];
348                 }
349         }
350         buffer[utf_get_number_of_u2s(u)] = '\0';
351 }
352
353
354 /* create headerfile for classes and store native methods in chain ************/
355
356 void headerfile_generate(classinfo *c, char *opt_directory)
357 {
358         char header_filename[1024] = "";
359         char classname[1024]; 
360         char uclassname[1024];
361         int32_t i;
362         methodinfo *m;                  
363         int32_t j;
364         methodinfo *m2;
365         bool nativelyoverloaded;
366
367         /* prevent compiler warnings */
368
369         nativelyoverloaded = false;
370
371         /* open headerfile for class */
372         gen_header_filename(classname, c->name);
373
374         /* create chain for renaming fields */
375         ident_chain = chain_new();
376         
377         if (opt_directory) {
378                 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
379
380         } else {
381                 sprintf(header_filename, "%s.h", classname);
382         }
383
384         file = fopen(header_filename, "w");
385         if (!file) {
386                 log_text("Can not open file to store header information");
387                 assert(0);
388         }
389
390         fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
391
392         /* convert to uppercase */
393         for (i = 0; classname[i]; i++) {
394                 uclassname[i] = toupper(classname[i]);
395         }
396         uclassname[i] = '\0';
397
398         fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
399
400         /* create structure for direct access to objects */     
401         fprintf(file, "/* Structure information for class: ");
402         utf_fprint_printable_ascii(file, c->name);
403         fprintf(file, " */\n\n");
404         fprintf(file, "typedef struct %s", HEAP_PREFIX);
405         printID(c->name);
406         fprintf(file, " {\n");
407         outputsize = 0;
408         dopadding = true;
409
410         printfields(c);
411
412         fprintf(file, "} %s", HEAP_PREFIX);
413         printID(c->name);
414         fprintf(file, ";\n\n");
415
416 #if defined(ENABLE_HANDLES)
417         /* create structure for indirection cell */
418         fprintf(file, "typedef struct ");
419         printID(c->name);
420         fprintf(file, " {\n");
421         fprintf(file, "   %s", HEAP_PREFIX);
422         printID(c->name);
423         fprintf(file, " *heap_object;\n");
424         fprintf(file, "} ");
425         printID(c->name);
426         fprintf(file, ";\n\n");
427 #endif
428
429         /* create chain for renaming overloaded methods */
430         chain_free(ident_chain);
431         ident_chain = chain_new();
432
433         /* create method-prototypes */
434                                 
435         /* find overloaded methods */
436
437         for (i = 0; i < c->methodscount; i++) {
438                 m = &(c->methods[i]);
439
440                 if (!(m->flags & ACC_NATIVE))
441                         continue;
442
443                 /* We use a dummy flag here. */
444
445                 if (!(m->flags & ACC_NATIVELY_OVERLOADED)) {
446                         nativelyoverloaded = false;
447
448                         for (j = i + 1; j < c->methodscount; j++) {
449                                 m2 = &(c->methods[j]);
450
451                                 if (!(m2->flags & ACC_NATIVE))
452                                         continue;
453
454                                 if (m->name == m2->name) {
455                                         m2->flags          |= ACC_NATIVELY_OVERLOADED;
456                                         nativelyoverloaded  = true;
457                                 }
458                         }
459                 }
460
461                 if (nativelyoverloaded == true)
462                         m->flags |= ACC_NATIVELY_OVERLOADED;
463         }
464
465         for (i = 0; i < c->methodscount; i++) {
466                 m = &(c->methods[i]);
467
468                 if (m->flags & ACC_NATIVE)
469                         printmethod(m);
470         }
471
472         chain_free(ident_chain);
473
474         fprintf(file, "#endif\n\n");
475
476         fclose(file);
477 }
478
479
480 /******** print classname, '$' used to seperate inner-class name ***********/
481
482 void print_classname(classinfo *clazz)
483 {
484         utf *u = clazz->name;
485     char *endpos  = u->text + u->blength;
486     char *utf_ptr = u->text; 
487         uint16_t c;
488
489     while (utf_ptr < endpos) {
490                 if ((c = utf_nextu2(&utf_ptr)) == '_')
491                         putc('$', file);
492                 else
493                         putc(c, file);
494         }
495
496
497
498 /*
499  * These are local overrides for various environment variables in Emacs.
500  * Please do not remove this and leave it at the end of the file, where
501  * Emacs will automagically detect them.
502  * ---------------------------------------------------------------------
503  * Local variables:
504  * mode: c
505  * indent-tabs-mode: t
506  * c-basic-offset: 4
507  * tab-width: 4
508  * End:
509  * vim:noexpandtab:sw=4:ts=4:
510  */