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