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