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