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