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