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