* Removed all Id tags.
[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 chain *ident_chain;     /* chain with method and field names in current class */
65 FILE *file = NULL;
66 static uint32_t outputsize;
67 static bool dopadding;
68
69
70 static void printIDpart(int c)
71 {
72         if ((c >= 'a' && c <= 'z') ||
73                 (c >= 'A' && c <= 'Z') ||
74                 (c >= '0' && c <= '9') ||
75                 (c == '_'))
76                 putc(c, file);
77         else
78                 putc('_', file);
79 }
80
81
82 void printID(utf *u)
83 {
84         char *utf_ptr = u->text;
85         int i;
86
87         for (i = 0; i < utf_get_number_of_u2s(u); i++) 
88                 printIDpart(utf_nextu2(&utf_ptr));
89 }
90
91
92 static void addoutputsize(int len)
93 {
94         uint32_t newsize;
95         int32_t  i;
96
97         if (!dopadding)
98                 return;
99
100         newsize = MEMORY_ALIGN(outputsize, len);
101         
102         for (i = outputsize; i < newsize; i++)
103                 fprintf(file, "   uint8_t pad%d\n", (int) i);
104
105         outputsize = newsize;
106 }
107
108
109 void printOverloadPart(utf *desc)
110 {
111         char *utf_ptr=desc->text;
112         uint16_t c;
113
114         fprintf(file, "__");
115
116         while ((c = utf_nextu2(&utf_ptr)) != ')') {
117                 switch (c) {
118                 case 'I':
119                 case 'S':
120                 case 'B':
121                 case 'C':
122                 case 'Z':
123                 case 'J':
124                 case 'F':
125                 case 'D': 
126                         fprintf(file, "%c", (char) c);
127                         break;
128                 case '[':
129                         fprintf(file, "_3");
130                         break;
131                 case 'L':
132                         putc('L', file);
133                         while ((c = utf_nextu2(&utf_ptr)) != ';')
134                                 printIDpart(c);
135                         fprintf(file, "_2");
136                         break;
137                 case '(':
138                         break;
139                 default: 
140                         log_text("invalid method descriptor");
141                         assert(0);
142                 }
143         }
144 }
145
146 static char *printtype(char *utf_ptr)
147 {
148         uint16_t c;
149
150         switch (utf_nextu2(&utf_ptr)) {
151         case 'V':
152                 fprintf(file, "void");
153                 break;
154         case 'I':
155         case 'S':
156         case 'B':
157         case 'C':
158         case 'Z':
159                 addoutputsize(4);
160                 fprintf(file, "int32_t");
161                 break;
162         case 'J':
163                 addoutputsize(8);
164                 fprintf(file, "int64_t");
165                 break;
166         case 'F':
167                 addoutputsize(4);
168                 fprintf(file, "float");
169                 break;
170         case 'D':
171                 addoutputsize(8);
172                 fprintf(file, "double");
173                 break;
174         case '[':
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;
185                                 
186                 case '[': fprintf(file, "java_objectarray_t*");
187                         while ((c = utf_nextu2(&utf_ptr)) == '[');
188                         if (c == 'L')
189                                 while (utf_nextu2(&utf_ptr) != ';');
190                         break;
191                            
192                 case 'L':  fprintf(file, "java_objectarray_t*");
193                         while (utf_nextu2(&utf_ptr) != ';');
194                         break;
195                 default:
196                         log_text("invalid type descriptor");
197                         assert(0);
198                 }
199                 break;
200                 
201         case 'L': 
202                 addoutputsize ( sizeof(java_object_t*));
203                 fprintf (file, "struct ");
204                 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);     
205                 fprintf (file, "*");
206                 break;
207                                         
208         default:
209                 log_text("Unknown type in field descriptor");
210                 assert(0);
211         }
212         
213         return utf_ptr;
214 }
215
216
217 /***** determine the number of entries of a utf string in the ident chain *****/
218
219 static int searchidentchain_utf(utf *ident) 
220 {
221         utf *u = chain_first(ident_chain);     /* first element of list */
222         int count = 0;
223
224         while (u) {
225                 if (u==ident) count++;         /* string found */
226                 u = chain_next(ident_chain);   /* next element in list */ 
227         }
228
229         return count;
230 }
231
232
233 /************** print structure for direct access to objects ******************/
234
235 static void printfields(classinfo *c)
236 {
237         int32_t i;
238         fieldinfo *f;
239         int ident_count;
240         
241         if (!c) {
242                 addoutputsize(sizeof(java_object_t));
243                 fprintf(file, "   java_object_t header;\n");
244                 return;
245         }
246                 
247         printfields(c->super.cls);
248         
249         for (i = 0; i < c->fieldscount; i++) {
250                 f = &(c->fields[i]);
251                 
252                 if (!(f->flags & ACC_STATIC)) {
253                         fprintf(file, "   ");
254                         printtype(f->descriptor->text);
255                         fprintf(file, " ");
256                         utf_fprint_printable_ascii(file, f->name);
257
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);    
262
263                         fprintf(file, ";\n");
264                 }
265         }
266 }
267
268
269 /***************** store prototype for native method in file ******************/
270
271 void printmethod(methodinfo *m)
272 {
273         char *utf_ptr;
274         int32_t paramnum = 1;
275
276         /* search for return-type in descriptor */      
277         utf_ptr = m->descriptor->text;
278         while (utf_nextu2(&utf_ptr) != ')');
279
280         /* create remarks */
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");
288
289         /* create prototype */                  
290         fprintf(file, "JNIEXPORT ");
291         printtype(utf_ptr);
292         fprintf(file, " JNICALL Java_");
293         printID(m->class->name);
294
295         chain_addlast(ident_chain, m->name);
296
297         fprintf(file, "_");
298         printID(m->name);
299
300         /* ATTENTION: We use a dummy flag here. */
301
302         if (m->flags & ACC_NATIVELY_OVERLOADED)
303                 printOverloadPart(m->descriptor);
304
305         fprintf(file, "(JNIEnv *env");
306         
307         utf_ptr = m->descriptor->text + 1;
308                         
309         if (!(m->flags & ACC_STATIC)) {
310                 fprintf(file, ", struct ");
311                 printID(m->class->name);
312                 fprintf(file, "* this");
313
314         } else {
315                 fprintf(file, ", jclass clazz");
316         }
317
318         if ((*utf_ptr) != ')') fprintf(file, ", ");
319                         
320         while ((*utf_ptr) != ')') {
321                 utf_ptr = printtype(utf_ptr);
322                 fprintf(file, " par%d", paramnum++);
323                 if ((*utf_ptr)!=')') fprintf(file, ", ");
324         }
325                         
326         fprintf(file, ");\n\n");
327 }
328
329
330 /******* remove package-name in fully-qualified classname *********************/
331
332 void gen_header_filename(char *buffer, utf *u)
333 {
334         int32_t i;
335   
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 '_' */
339
340                 } else {
341                         buffer[i] = u->text[i];
342                 }
343         }
344         buffer[utf_get_number_of_u2s(u)] = '\0';
345 }
346
347
348 /* create headerfile for classes and store native methods in chain ************/
349
350 void headerfile_generate(classinfo *c, char *opt_directory)
351 {
352         char header_filename[1024] = "";
353         char classname[1024]; 
354         char uclassname[1024];
355         int32_t i;
356         methodinfo *m;                  
357         int32_t j;
358         methodinfo *m2;
359         bool nativelyoverloaded;
360
361         /* prevent compiler warnings */
362
363         nativelyoverloaded = false;
364
365         /* open headerfile for class */
366         gen_header_filename(classname, c->name);
367
368         /* create chain for renaming fields */
369         ident_chain = chain_new();
370         
371         if (opt_directory) {
372                 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
373
374         } else {
375                 sprintf(header_filename, "%s.h", classname);
376         }
377
378         file = fopen(header_filename, "w");
379         if (!file) {
380                 log_text("Can not open file to store header information");
381                 assert(0);
382         }
383
384         fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
385
386         /* convert to uppercase */
387         for (i = 0; classname[i]; i++) {
388                 uclassname[i] = toupper(classname[i]);
389         }
390         uclassname[i] = '\0';
391
392         fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
393
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 ");
399         printID(c->name);                                                       
400         fprintf(file, " {\n");
401         outputsize = 0;
402         dopadding = true;
403
404         printfields(c);
405
406         fprintf(file, "} ");
407         printID(c->name);
408         fprintf(file, ";\n\n");
409
410         /* create chain for renaming overloaded methods */
411         chain_free(ident_chain);
412         ident_chain = chain_new();
413
414         /* create method-prototypes */
415                                 
416         /* find overloaded methods */
417
418         for (i = 0; i < c->methodscount; i++) {
419                 m = &(c->methods[i]);
420
421                 if (!(m->flags & ACC_NATIVE))
422                         continue;
423
424                 /* We use a dummy flag here. */
425
426                 if (!(m->flags & ACC_NATIVELY_OVERLOADED)) {
427                         nativelyoverloaded = false;
428
429                         for (j = i + 1; j < c->methodscount; j++) {
430                                 m2 = &(c->methods[j]);
431
432                                 if (!(m2->flags & ACC_NATIVE))
433                                         continue;
434
435                                 if (m->name == m2->name) {
436                                         m2->flags          |= ACC_NATIVELY_OVERLOADED;
437                                         nativelyoverloaded  = true;
438                                 }
439                         }
440                 }
441
442                 if (nativelyoverloaded == true)
443                         m->flags |= ACC_NATIVELY_OVERLOADED;
444         }
445
446         for (i = 0; i < c->methodscount; i++) {
447                 m = &(c->methods[i]);
448
449                 if (m->flags & ACC_NATIVE)
450                         printmethod(m);
451         }
452
453         chain_free(ident_chain);
454
455         fprintf(file, "#endif\n\n");
456
457         fclose(file);
458 }
459
460
461 /******** print classname, '$' used to seperate inner-class name ***********/
462
463 void print_classname(classinfo *clazz)
464 {
465         utf *u = clazz->name;
466     char *endpos  = u->text + u->blength;
467     char *utf_ptr = u->text; 
468         uint16_t c;
469
470     while (utf_ptr < endpos) {
471                 if ((c = utf_nextu2(&utf_ptr)) == '_')
472                         putc('$', file);
473                 else
474                         putc(c, file);
475         }
476
477
478
479 /*
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  * ---------------------------------------------------------------------
484  * Local variables:
485  * mode: c
486  * indent-tabs-mode: t
487  * c-basic-offset: 4
488  * tab-width: 4
489  * End:
490  * vim:noexpandtab:sw=4:ts=4:
491  */