no more unloaded classinfos!
[cacao.git] / src / cacaoh / headers.c
1 /* src/cacaoh/headers.c - functions for header generation
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Reinhard Grafl
28
29    Changes: Mark Probst
30             Philipp Tomsich
31             Christian Thalinger
32
33    $Id: headers.c 2193 2005-04-02 19:33:43Z edwin $
34
35 */
36
37
38 #include <stdlib.h>
39 #include <string.h>
40 #include <ctype.h>
41
42 #include "config.h"
43
44 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
45 # if defined(__DARWIN__)
46 #  include <signal.h>
47 # endif
48 # include <ucontext.h>
49 #endif
50
51 #include "mm/boehm.h"
52 #include "mm/memory.h"
53 #include "native/include/java_lang_String.h"
54 #include "native/include/java_lang_Throwable.h"
55 #include "toolbox/chain.h"
56 #include "toolbox/logging.h"
57 #include "vm/class.h"
58 #include "vm/global.h"
59 #include "vm/method.h"
60 #include "vm/tables.h"
61 #include "vm/loader.h"
62 #include "vm/options.h"
63 #include "vm/builtin.h"
64 #include "vm/jit/asmpart.h"
65
66
67 /******* replace some external functions  *********/
68  
69 functionptr native_findfunction(utf *cname, utf *mname, utf *desc, bool isstatic)
70 { return NULL; }
71
72 java_objectheader *native_new_and_init(classinfo *c) { return NULL; }
73 java_objectheader *native_new_and_init_string(classinfo *c, java_lang_String *s) { return NULL; }
74 java_objectheader *native_new_and_init_int(classinfo *c, s4 i) { return NULL; }
75 java_objectheader *native_new_and_init_throwable(classinfo *c, java_lang_Throwable *t) { return NULL; }
76
77
78 java_objectheader *asm_calljavafunction(methodinfo *m, void *arg1, void *arg2,
79                                                                                 void *arg3, void *arg4) { return NULL; }
80
81 void asm_call_jit_compiler(void) {}
82
83 s4 asm_builtin_checkcast(java_objectheader *obj, classinfo *class) { return 0; }
84
85 s4 asm_builtin_idiv(s4 a, s4 b) { return 0; }
86 s4 asm_builtin_irem(s4 a, s4 b) { return 0; }
87 s8 asm_builtin_ldiv(s8 a, s8 b) { return 0; }
88 s8 asm_builtin_lrem(s8 a, s8 b) { return 0; }
89
90 s4 asm_builtin_f2i(float a) { return 0; }
91 s8 asm_builtin_f2l(float a) { return 0; }
92 s4 asm_builtin_d2i(double a) { return 0; }
93 s8 asm_builtin_d2l(double a) { return 0; }
94
95 void use_class_as_object(void) {}
96 void asm_builtin_monitorenter(java_objectheader *o) {}
97 void *asm_builtin_monitorexit(java_objectheader *o) { return NULL; }
98
99 s4 asm_builtin_checkarraycast(java_objectheader *obj, vftbl_t *target) {
100         return 0;
101 }
102
103 #if defined(__MIPS__)
104 long compare_and_swap(long *p, long oldval, long newval)
105 {
106         if (*p == oldval) {
107                 *p = newval;
108                 return oldval;
109         } else
110                 return *p;
111 }
112 #endif
113
114
115 java_objectheader *asm_builtin_new(classinfo *c) { return NULL; }
116
117 #if defined(__I386__)
118 s4 asm_builtin_arrayinstanceof(java_objectheader *obj, classinfo *class) {
119         return 0;
120 }
121 void asm_builtin_newarray(s4 size, vftbl_t *arrayvftbl) {}
122 #endif
123
124 void asm_builtin_aastore(java_objectarray *a, s4 index, java_objectheader *o) {}
125
126 u1 *createcompilerstub(methodinfo *m) { return NULL; }
127 u1 *createnativestub(functionptr f, methodinfo *m) { return NULL; }
128 u1 *oldcreatenativestub(functionptr f, methodinfo *m) { return NULL; }
129
130 void removecompilerstub(u1 *stub) {}
131 void removenativestub(u1 *stub) {}
132
133 void asm_perform_threadswitch(u1 **from, u1 **to, u1 **stackTop) {}
134 u1* asm_initialize_thread_stack(void *func, u1 *stack) { return NULL; }
135
136 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
137
138 void asm_handle_builtin_exception(classinfo *c) {}
139 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
140
141 #if defined(__DARWIN__)
142 int cacao_catch_Handler(void) {}
143 #endif
144
145 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
146 threadcritnode asm_criticalsections;
147 void thread_restartcriticalsection(ucontext_t *uc) {}
148 #endif
149
150
151 void setVMClassField(classinfo *c) {}
152 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
153 {
154         return NULL;
155 }
156
157 tristate_t
158 typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
159 {
160         return true;
161 }
162
163 /************************ global variables **********************/
164
165 chain *nativemethod_chain;              /* chain with native methods          */
166 chain *nativeclass_chain;               /* chain with processed classes       */
167 static chain *ident_chain; /* chain with method and field names in current class */
168 FILE *file = NULL;
169 static u4 outputsize;
170 static bool dopadding;
171
172
173 static void printIDpart(int c)
174 {
175         if ((c >= 'a' && c <= 'z')
176                 || (c >= 'A' && c <= 'Z')
177                 || (c >= '0' && c <= '9')
178                 || (c == '_'))
179                 putc(c, file);
180         else
181                 putc('_', file);
182 }
183
184
185 static void printID(utf *u)
186 {
187         char *utf_ptr = u->text;
188         int i;
189
190         for (i = 0; i < utf_strlen(u); i++) 
191                 printIDpart(utf_nextu2(&utf_ptr));
192 }
193
194
195 static void addoutputsize (int len)
196 {
197         u4 newsize,i;
198         if (!dopadding) return;
199
200         newsize = ALIGN(outputsize, len);
201         
202         for (i = outputsize; i < newsize; i++) fprintf(file, "   u1 pad%d\n", (int) i);
203         outputsize = newsize;
204 }
205
206
207 static void printOverloadPart(utf *desc)
208 {
209         char *utf_ptr=desc->text;
210         u2 c;
211
212         fprintf(file,"__");
213         while ((c=utf_nextu2(&utf_ptr))!=')') {
214                 switch (c) {
215                         case 'I':
216                         case 'S':
217                         case 'B':
218                         case 'C':
219                         case 'Z':
220                         case 'J':
221                         case 'F':
222                         case 'D': 
223                                 fprintf (file, "%c",(char)c);
224                                 break;
225                         case '[':
226                                 fprintf(file,"_3");
227                                 break;
228                         case 'L':
229                                 putc('L',file);
230                                 while ( (c=utf_nextu2(&utf_ptr)) != ';')
231                                         printIDpart (c);
232                                 fprintf(file,"_2");
233                                 break;
234                         case '(':
235                                 break;
236                         default: panic ("invalid method descriptor");
237                 }
238         }
239 }
240
241 static char *printtype(char *utf_ptr)
242 {
243         u2 c;
244
245         switch (utf_nextu2(&utf_ptr)) {
246         case 'V': fprintf (file, "void");
247                 break;
248         case 'I':
249         case 'S':
250         case 'B':
251         case 'C':
252         case 'Z': addoutputsize (4);
253                 fprintf (file, "s4");
254                 break;
255         case 'J': addoutputsize (8);
256                 fprintf (file, "s8");
257                 break;
258         case 'F': addoutputsize (4);
259                 fprintf (file, "float");
260                 break;
261         case 'D': addoutputsize (8);
262                 fprintf (file, "double");
263                 break;
264         case '[':
265                 addoutputsize ( sizeof(java_arrayheader*) ); 
266                 switch (utf_nextu2(&utf_ptr)) {
267                 case 'I':  fprintf (file, "java_intarray*"); break;
268                 case 'J':  fprintf (file, "java_longarray*"); break;
269                 case 'Z':  fprintf (file, "java_booleanarray*"); break;
270                 case 'B':  fprintf (file, "java_bytearray*"); break;
271                 case 'S':  fprintf (file, "java_shortarray*"); break;
272                 case 'C':  fprintf (file, "java_chararray*"); break;
273                 case 'F':  fprintf (file, "java_floatarray*"); break;
274                 case 'D':  fprintf (file, "java_doublearray*"); break;
275                                 
276                 case '[':  fprintf (file, "java_objectarray*");                                        
277                         while ((c = utf_nextu2(&utf_ptr)) == '[') ;
278                         if (c=='L') 
279                                 while (utf_nextu2(&utf_ptr) != ';');
280                         break;
281                            
282                 case 'L':  fprintf (file, "java_objectarray*");
283                         while ( utf_nextu2(&utf_ptr) != ';');
284                         break;
285                 default: panic ("invalid type descriptor");
286                 }
287                 break;
288                 
289         case 'L': 
290                 addoutputsize ( sizeof(java_objectheader*));
291                 fprintf (file, "struct ");
292                 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);     
293                 fprintf (file, "*");
294                 break;
295                                         
296         default:  panic ("Unknown type in field descriptor");
297         }
298         
299         return utf_ptr;
300 }
301
302
303 /***** determine the number of entries of a utf string in the ident chain *****/
304
305 static int searchidentchain_utf(utf *ident) 
306 {
307         utf *u = chain_first(ident_chain);     /* first element of list */
308         int count = 0;
309
310         while (u) {
311                 if (u==ident) count++;         /* string found */
312                 u = chain_next(ident_chain);   /* next element in list */ 
313         }
314
315         return count;
316 }
317
318
319 /************** print structure for direct access to objects ******************/
320
321 static void printfields(classinfo *c)
322 {
323         u4 i;
324         fieldinfo *f;
325         int ident_count;
326         
327         if (!c) {
328                 addoutputsize(sizeof(java_objectheader));
329                 fprintf(file, "   java_objectheader header;\n");
330                 return;
331         }
332                 
333         printfields(c->super.cls);
334         
335         for (i = 0; i < c->fieldscount; i++) {
336                 f = &(c->fields[i]);
337                 
338                 if (!(f->flags & ACC_STATIC)) {
339                         fprintf(file, "   ");
340                         printtype(f->descriptor->text);
341                         fprintf(file, " ");
342                         utf_fprint(file, f->name);
343
344                         /* rename multiple fieldnames */
345                         if ((ident_count = searchidentchain_utf(f->name)))
346                                 fprintf(file, "%d", ident_count - 1);
347                         chain_addlast(ident_chain, f->name);    
348
349                         fprintf(file, ";\n");
350                 }
351         }
352 }
353
354
355 /***************** store prototype for native method in file ******************/
356
357 void printmethod(methodinfo *m)
358 {
359         char *utf_ptr;
360         u2 paramnum = 1;
361
362         /* search for return-type in descriptor */      
363         utf_ptr = m->descriptor->text;
364         while (utf_nextu2(&utf_ptr) != ')');
365
366         /* create remarks */
367         fprintf(file, "\n/*\n * Class:     ");
368         utf_fprint(file, m->class->name);
369         fprintf(file, "\n * Method:    ");
370         utf_fprint(file, m->name);
371         fprintf(file, "\n * Signature: ");
372         utf_fprint(file, m->descriptor);
373         fprintf(file, "\n */\n");       
374
375         /* create prototype */                  
376         fprintf(file, "JNIEXPORT ");                            
377         printtype(utf_ptr);
378         fprintf(file, " JNICALL Java_");
379         printID(m->class->name);           
380
381         chain_addlast(ident_chain, m->name);    
382
383         fprintf(file, "_");
384         printID(m->name);
385         if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
386         fprintf(file, "(JNIEnv *env");
387         
388         utf_ptr = m->descriptor->text + 1;
389                         
390         if (!(m->flags & ACC_STATIC)) {
391                 fprintf(file, ", struct ");
392                 printID(m->class->name);
393                 fprintf(file, "* this");
394
395         } else {
396                 fprintf(file, ", jclass clazz");
397         }
398
399         if ((*utf_ptr) != ')') fprintf(file, ", ");
400                         
401         while ((*utf_ptr) != ')') {
402                 utf_ptr = printtype(utf_ptr);
403                 fprintf(file, " par%d", paramnum++);
404                 if ((*utf_ptr)!=')') fprintf(file, ", ");
405         }
406                         
407         fprintf(file, ");\n\n");
408 }
409
410
411 /******* remove package-name in fully-qualified classname *********************/
412
413 void gen_header_filename(char *buffer, utf *u)
414 {
415         s4 i;
416   
417         for (i = 0; i < utf_strlen(u); i++) {
418                 if ((u->text[i] == '/') || (u->text[i] == '$')) {
419                         buffer[i] = '_';  /* convert '$' and '/' to '_' */
420
421                 } else {
422                         buffer[i] = u->text[i];
423                 }
424         }
425         buffer[utf_strlen(u)] = '\0';
426 }
427
428
429 /* create headerfile for classes and store native methods in chain ************/
430
431 void headerfile_generate(classinfo *c, char *opt_directory)
432 {
433         char header_filename[1024] = "";
434         char classname[1024]; 
435         char uclassname[1024];
436         u2 i;
437         methodinfo *m;                  
438         u2 i2;
439         methodinfo *m2;
440         u2 nativelyoverloaded;                  
441                       
442         /* store class in chain */                    
443         chain_addlast(nativeclass_chain, c);
444                                 
445         /* open headerfile for class */
446         gen_header_filename(classname, c->name);
447
448         /* create chain for renaming fields */
449         ident_chain = chain_new();
450         
451         if (opt_directory) {
452                 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
453
454         } else {
455                 sprintf(header_filename, "%s.h", classname);
456         }
457
458         file = fopen(header_filename, "w");
459         if (!file)
460                 panic("Can not open file to store header information");
461
462         fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n");
463
464         /* convert to uppercase */
465         for (i = 0; classname[i]; i++) {
466                 uclassname[i] = toupper(classname[i]);
467         }
468         uclassname[i] = '\0';
469
470         fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
471
472         /* create structure for direct access to objects */     
473         fprintf(file, "/* Structure information for class: ");
474         utf_fprint(file, c->name);
475         fprintf(file, " */\n\n");
476         fprintf(file, "typedef struct ");
477         printID(c->name);                                                       
478         fprintf(file, " {\n");
479         outputsize = 0;
480         dopadding = true;
481
482         printfields(c);
483
484         fprintf(file, "} ");
485         printID(c->name);
486         fprintf(file, ";\n\n");
487
488         /* create chain for renaming overloaded methods */
489         chain_free(ident_chain);
490         ident_chain = chain_new();
491
492         /* create method-prototypes */
493                                 
494         /* find overloaded methods */
495         for (i = 0; i < c->methodscount; i++) {
496
497                 m = &(c->methods[i]);
498
499                 if (!(m->flags & ACC_NATIVE)) continue;
500                 if (!m->nativelyoverloaded) {
501                         nativelyoverloaded=false;
502                         for (i2=i+1;i2<c->methodscount; i2++) {
503                                 m2 = &(c->methods[i2]);
504                                 if (!(m2->flags & ACC_NATIVE)) continue;
505                                 if (m->name==m2->name) {
506                                         m2->nativelyoverloaded=true;
507                                         nativelyoverloaded=true;
508                                 }
509                         }
510                         m->nativelyoverloaded=nativelyoverloaded;
511                 }
512
513         }
514
515         for (i = 0; i < c->methodscount; i++) {
516
517                 m = &(c->methods[i]);
518
519                 if (m->flags & ACC_NATIVE) {
520                         chain_addlast(nativemethod_chain, m);
521                         printmethod(m);
522                 }
523         }
524
525         chain_free(ident_chain);
526
527         fprintf(file, "#endif\n\n");
528
529         fclose(file);
530 }
531
532
533 /******** print classname, '$' used to seperate inner-class name ***********/
534
535 void print_classname(classinfo *clazz)
536 {
537         utf *u = clazz->name;
538     char *endpos  = u->text + u->blength;
539     char *utf_ptr = u->text; 
540         u2 c;
541
542     while (utf_ptr < endpos) {
543                 if ((c = utf_nextu2(&utf_ptr)) == '_') {
544                         putc('$', file);
545
546                 } else {
547                         putc(c, file);
548                 }
549         }
550
551
552
553 /*
554  * These are local overrides for various environment variables in Emacs.
555  * Please do not remove this and leave it at the end of the file, where
556  * Emacs will automagically detect them.
557  * ---------------------------------------------------------------------
558  * Local variables:
559  * mode: c
560  * indent-tabs-mode: t
561  * c-basic-offset: 4
562  * tab-width: 4
563  * End:
564  */