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