Fixed: missing return statement at end of non-void function
[cacao.git] / src / cacaoh / headers.c
1 /* src/cacaoh/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 2518 2005-05-23 11:57:59Z twisti $
34
35 */
36
37
38 #include <assert.h>
39 #include <ctype.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include "config.h"
44
45 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
46 # if defined(__DARWIN__)
47 #  include <signal.h>
48 # endif
49 # include <ucontext.h>
50 #endif
51
52 #include "mm/boehm.h"
53 #include "mm/memory.h"
54 #include "native/include/java_lang_String.h"
55 #include "native/include/java_lang_Throwable.h"
56 #include "toolbox/chain.h"
57 #include "toolbox/logging.h"
58 #include "vm/builtin.h"
59 #include "vm/class.h"
60 #include "vm/global.h"
61 #include "vm/method.h"
62 #include "vm/tables.h"
63 #include "vm/loader.h"
64 #include "vm/options.h"
65 #include "vm/stringlocal.h"
66 #include "vm/jit/asmpart.h"
67
68
69 /******* replace some external functions  *********/
70  
71 functionptr native_findfunction(utf *cname, utf *mname, utf *desc, bool isstatic)
72 { return NULL; }
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
80 java_objectheader *asm_calljavafunction(methodinfo *m, void *arg1, void *arg2,
81                                                                                 void *arg3, void *arg4) { return NULL; }
82
83 void asm_call_jit_compiler(void) {}
84
85 /* code patching functions */
86 void patcher_builtin_new(u1 *sp) {}
87 void patcher_builtin_newarray(u1 *sp) {}
88 void patcher_builtin_arraycheckcast(u1 *sp) {}
89 void patcher_builtin_arrayinstanceof(u1 *sp) {}
90
91 s4 asm_builtin_idiv(s4 a, s4 b) { return 0; }
92 s4 asm_builtin_irem(s4 a, s4 b) { return 0; }
93 s8 asm_builtin_ldiv(s8 a, s8 b) { return 0; }
94 s8 asm_builtin_lrem(s8 a, s8 b) { return 0; }
95
96 s4 asm_builtin_f2i(float a) { return 0; }
97 s8 asm_builtin_f2l(float a) { return 0; }
98 s4 asm_builtin_d2i(double a) { return 0; }
99 s8 asm_builtin_d2l(double a) { return 0; }
100
101 void use_class_as_object(void) {}
102 void asm_builtin_monitorenter(java_objectheader *o) {}
103 void *asm_builtin_monitorexit(java_objectheader *o) { return NULL; }
104
105 s4 asm_builtin_arraycheckcast(java_objectheader *obj, vftbl_t *target)
106 {
107         return 0;
108 }
109
110 #if defined(__MIPS__)
111 long compare_and_swap(long *p, long oldval, long newval)
112 {
113         if (*p == oldval) {
114                 *p = newval;
115                 return oldval;
116         } else
117                 return *p;
118 }
119 #endif
120
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
134 void *asm_switchstackandcall(void *stack, void *func, void **stacktopsave, void * p) { return NULL; }
135
136 void asm_handle_builtin_exception(classinfo *c) {}
137 void asm_getclassvalues_atomic(vftbl_t *super, vftbl_t *sub, castinfo *out) {}
138
139 #if defined(__DARWIN__)
140 int cacao_catch_Handler(void) {}
141 #endif
142
143 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
144 threadcritnode asm_criticalsections;
145 void thread_restartcriticalsection(ucontext_t *uc) {}
146 #endif
147
148
149 void setVMClassField(classinfo *c) {}
150 void *Java_java_lang_VMObject_clone(void *env, void *clazz, void * this)
151 {
152         return NULL;
153 }
154
155 tristate_t
156 typeinfo_is_assignable_to_class(typeinfo *value,classref_or_classinfo dest)
157 {
158         return true;
159 }
160
161
162 /* exception functions ********************************************************/
163
164 /* these should not be called */
165
166 void throw_exception(void) { assert(0); }
167 void throw_exception_exit(void) { assert(0); }
168
169 java_objectheader *new_verifyerror(methodinfo *m, const char *message)
170 {
171         assert(0);
172
173         /* keep compiler happy */
174
175         return NULL;
176 }
177
178 java_objectheader *new_exception_throwable(const char *classname, java_lang_Throwable *throwable)
179 {
180         assert(0);
181
182         /* keep compiler happy */
183
184         return NULL;
185 }
186
187
188 void throw_cacao_exception_exit(const char *exception, const char *message, ...)
189 {
190         va_list ap;
191
192         fprintf(stderr, "%s: ", exception);
193
194         va_start(ap, message);
195         vfprintf(stderr, message, ap);
196         va_end(ap);
197
198         exit(1);
199 }
200
201
202 java_objectheader *new_exception(const char *classname)
203 {
204         fprintf(stderr, "%s\n", classname);
205         exit(1);
206
207         /* keep compiler happy */
208
209         return NULL;
210 }
211
212
213 java_objectheader *new_exception_message(const char *classname, const char *message)
214 {
215         fprintf(stderr, "%s: %s\n", classname, message);
216         exit(1);
217
218         /* keep compiler happy */
219
220         return NULL;
221 }
222
223
224 java_objectheader *new_exception_utfmessage(const char *classname, utf *message)
225 {
226         fprintf(stderr, "%s: ", classname);
227         utf_display(message);
228         fputc('\n', stderr);
229
230         exit(1);
231
232         /* keep compiler happy */
233
234         return NULL;
235 }
236
237
238 java_objectheader *new_classformaterror(classinfo *c, const char *message, ...)
239 {
240         va_list ap;
241
242         utf_display(c->name);
243         fprintf(stderr, ": ");
244
245         va_start(ap, message);
246         vfprintf(stderr, message, ap);
247         va_end(ap);
248
249         fputc('\n', stderr);
250
251         exit(1);
252
253         /* keep compiler happy */
254
255         return NULL;
256 }
257
258
259 java_objectheader *new_internalerror(const char *message, ...)
260 {
261         va_list ap;
262
263         fprintf(stderr, "%s: ", string_java_lang_InternalError);
264
265         va_start(ap, message);
266         vfprintf(stderr, message, ap);
267         va_end(ap);
268
269         exit(1);
270
271         /* keep compiler happy */
272
273         return NULL;
274 }
275
276
277 java_objectheader *new_unsupportedclassversionerror(classinfo *c, const char *message, ...)
278 {
279         va_list ap;
280
281         fprintf(stderr, "%s: ", string_java_lang_UnsupportedClassVersionError);
282
283         utf_display(c->name);
284         fprintf(stderr, ": ");
285
286         va_start(ap, message);
287         vfprintf(stderr, message, ap);
288         va_end(ap);
289
290         exit(1);
291
292         /* keep compiler happy */
293
294         return NULL;
295 }
296
297
298 java_objectheader *new_negativearraysizeexception(void)
299 {
300         fprintf(stderr, "%s", string_java_lang_NegativeArraySizeException);
301         exit(1);
302
303         /* keep compiler happy */
304
305         return NULL;
306 }
307
308
309 java_objectheader *new_nullpointerexception(void)
310 {
311         fprintf(stderr, "%s", string_java_lang_NullPointerException);
312         exit(1);
313
314         /* keep compiler happy */
315
316         return NULL;
317 }
318
319
320 /************************ global variables **********************/
321
322 chain *nativemethod_chain;              /* chain with native methods          */
323 chain *nativeclass_chain;               /* chain with processed classes       */
324 chain *ident_chain;     /* chain with method and field names in current class */
325 FILE *file = NULL;
326 static u4 outputsize;
327 static bool dopadding;
328
329
330 static void printIDpart(int c)
331 {
332         if ((c >= 'a' && c <= 'z') ||
333                 (c >= 'A' && c <= 'Z') ||
334                 (c >= '0' && c <= '9') ||
335                 (c == '_'))
336                 putc(c, file);
337         else
338                 putc('_', file);
339 }
340
341
342 void printID(utf *u)
343 {
344         char *utf_ptr = u->text;
345         int i;
346
347         for (i = 0; i < utf_strlen(u); i++) 
348                 printIDpart(utf_nextu2(&utf_ptr));
349 }
350
351
352 static void addoutputsize (int len)
353 {
354         u4 newsize,i;
355         if (!dopadding) return;
356
357         newsize = ALIGN(outputsize, len);
358         
359         for (i = outputsize; i < newsize; i++) fprintf(file, "   u1 pad%d\n", (int) i);
360         outputsize = newsize;
361 }
362
363
364 void printOverloadPart(utf *desc)
365 {
366         char *utf_ptr=desc->text;
367         u2 c;
368
369         fprintf(file, "__");
370
371         while ((c = utf_nextu2(&utf_ptr)) != ')') {
372                 switch (c) {
373                 case 'I':
374                 case 'S':
375                 case 'B':
376                 case 'C':
377                 case 'Z':
378                 case 'J':
379                 case 'F':
380                 case 'D': 
381                         fprintf(file, "%c", (char) c);
382                         break;
383                 case '[':
384                         fprintf(file, "_3");
385                         break;
386                 case 'L':
387                         putc('L', file);
388                         while ((c = utf_nextu2(&utf_ptr)) != ';')
389                                 printIDpart(c);
390                         fprintf(file, "_2");
391                         break;
392                 case '(':
393                         break;
394                 default: 
395                         log_text("invalid method descriptor");
396                         assert(0);
397                 }
398         }
399 }
400
401 static char *printtype(char *utf_ptr)
402 {
403         u2 c;
404
405         switch (utf_nextu2(&utf_ptr)) {
406         case 'V': fprintf (file, "void");
407                 break;
408         case 'I':
409         case 'S':
410         case 'B':
411         case 'C':
412         case 'Z': addoutputsize (4);
413                 fprintf (file, "s4");
414                 break;
415         case 'J': addoutputsize (8);
416                 fprintf (file, "s8");
417                 break;
418         case 'F': addoutputsize (4);
419                 fprintf (file, "float");
420                 break;
421         case 'D': addoutputsize (8);
422                 fprintf (file, "double");
423                 break;
424         case '[':
425                 addoutputsize ( sizeof(java_arrayheader*) ); 
426                 switch (utf_nextu2(&utf_ptr)) {
427                 case 'I':  fprintf (file, "java_intarray*"); break;
428                 case 'J':  fprintf (file, "java_longarray*"); break;
429                 case 'Z':  fprintf (file, "java_booleanarray*"); break;
430                 case 'B':  fprintf (file, "java_bytearray*"); break;
431                 case 'S':  fprintf (file, "java_shortarray*"); break;
432                 case 'C':  fprintf (file, "java_chararray*"); break;
433                 case 'F':  fprintf (file, "java_floatarray*"); break;
434                 case 'D':  fprintf (file, "java_doublearray*"); break;
435                                 
436                 case '[': fprintf(file, "java_objectarray*");
437                         while ((c = utf_nextu2(&utf_ptr)) == '[');
438                         if (c == 'L')
439                                 while (utf_nextu2(&utf_ptr) != ';');
440                         break;
441                            
442                 case 'L':  fprintf(file, "java_objectarray*");
443                         while (utf_nextu2(&utf_ptr) != ';');
444                         break;
445                 default:
446                         log_text("invalid type descriptor");
447                         assert(0);
448                 }
449                 break;
450                 
451         case 'L': 
452                 addoutputsize ( sizeof(java_objectheader*));
453                 fprintf (file, "struct ");
454                 while ( (c = utf_nextu2(&utf_ptr)) != ';' ) printIDpart (c);     
455                 fprintf (file, "*");
456                 break;
457                                         
458         default:
459                 log_text("Unknown type in field descriptor");
460                 assert(0);
461         }
462         
463         return utf_ptr;
464 }
465
466
467 /***** determine the number of entries of a utf string in the ident chain *****/
468
469 static int searchidentchain_utf(utf *ident) 
470 {
471         utf *u = chain_first(ident_chain);     /* first element of list */
472         int count = 0;
473
474         while (u) {
475                 if (u==ident) count++;         /* string found */
476                 u = chain_next(ident_chain);   /* next element in list */ 
477         }
478
479         return count;
480 }
481
482
483 /************** print structure for direct access to objects ******************/
484
485 static void printfields(classinfo *c)
486 {
487         u4 i;
488         fieldinfo *f;
489         int ident_count;
490         
491         if (!c) {
492                 addoutputsize(sizeof(java_objectheader));
493                 fprintf(file, "   java_objectheader header;\n");
494                 return;
495         }
496                 
497         printfields(c->super.cls);
498         
499         for (i = 0; i < c->fieldscount; i++) {
500                 f = &(c->fields[i]);
501                 
502                 if (!(f->flags & ACC_STATIC)) {
503                         fprintf(file, "   ");
504                         printtype(f->descriptor->text);
505                         fprintf(file, " ");
506                         utf_fprint(file, f->name);
507
508                         /* rename multiple fieldnames */
509                         if ((ident_count = searchidentchain_utf(f->name)))
510                                 fprintf(file, "%d", ident_count - 1);
511                         chain_addlast(ident_chain, f->name);    
512
513                         fprintf(file, ";\n");
514                 }
515         }
516 }
517
518
519 /***************** store prototype for native method in file ******************/
520
521 void printmethod(methodinfo *m)
522 {
523         char *utf_ptr;
524         u2 paramnum = 1;
525
526         /* search for return-type in descriptor */      
527         utf_ptr = m->descriptor->text;
528         while (utf_nextu2(&utf_ptr) != ')');
529
530         /* create remarks */
531         fprintf(file, "\n/*\n * Class:     ");
532         utf_fprint(file, m->class->name);
533         fprintf(file, "\n * Method:    ");
534         utf_fprint(file, m->name);
535         fprintf(file, "\n * Signature: ");
536         utf_fprint(file, m->descriptor);
537         fprintf(file, "\n */\n");
538
539         /* create prototype */                  
540         fprintf(file, "JNIEXPORT ");
541         printtype(utf_ptr);
542         fprintf(file, " JNICALL Java_");
543         printID(m->class->name);
544
545         chain_addlast(ident_chain, m->name);
546
547         fprintf(file, "_");
548         printID(m->name);
549         if (m->nativelyoverloaded) printOverloadPart(m->descriptor);
550         fprintf(file, "(JNIEnv *env");
551         
552         utf_ptr = m->descriptor->text + 1;
553                         
554         if (!(m->flags & ACC_STATIC)) {
555                 fprintf(file, ", struct ");
556                 printID(m->class->name);
557                 fprintf(file, "* this");
558
559         } else {
560                 fprintf(file, ", jclass clazz");
561         }
562
563         if ((*utf_ptr) != ')') fprintf(file, ", ");
564                         
565         while ((*utf_ptr) != ')') {
566                 utf_ptr = printtype(utf_ptr);
567                 fprintf(file, " par%d", paramnum++);
568                 if ((*utf_ptr)!=')') fprintf(file, ", ");
569         }
570                         
571         fprintf(file, ");\n\n");
572 }
573
574
575 /******* remove package-name in fully-qualified classname *********************/
576
577 void gen_header_filename(char *buffer, utf *u)
578 {
579         s4 i;
580   
581         for (i = 0; i < utf_strlen(u); i++) {
582                 if ((u->text[i] == '/') || (u->text[i] == '$')) {
583                         buffer[i] = '_';  /* convert '$' and '/' to '_' */
584
585                 } else {
586                         buffer[i] = u->text[i];
587                 }
588         }
589         buffer[utf_strlen(u)] = '\0';
590 }
591
592
593 /* create headerfile for classes and store native methods in chain ************/
594
595 void headerfile_generate(classinfo *c, char *opt_directory)
596 {
597         char header_filename[1024] = "";
598         char classname[1024]; 
599         char uclassname[1024];
600         u2 i;
601         methodinfo *m;                  
602         u2 i2;
603         methodinfo *m2;
604         u2 nativelyoverloaded;                  
605                       
606         /* store class in chain */                    
607         chain_addlast(nativeclass_chain, c);
608                                 
609         /* open headerfile for class */
610         gen_header_filename(classname, c->name);
611
612         /* create chain for renaming fields */
613         ident_chain = chain_new();
614         
615         if (opt_directory) {
616                 sprintf(header_filename, "%s/%s.h", opt_directory, classname);
617
618         } else {
619                 sprintf(header_filename, "%s.h", classname);
620         }
621
622         file = fopen(header_filename, "w");
623         if (!file) {
624                 log_text("Can not open file to store header information");
625                 assert(0);
626         }
627
628         fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
629
630         /* convert to uppercase */
631         for (i = 0; classname[i]; i++) {
632                 uclassname[i] = toupper(classname[i]);
633         }
634         uclassname[i] = '\0';
635
636         fprintf(file, "#ifndef _%s_H\n#define _%s_H\n\n", uclassname, uclassname);
637
638         /* create structure for direct access to objects */     
639         fprintf(file, "/* Structure information for class: ");
640         utf_fprint(file, c->name);
641         fprintf(file, " */\n\n");
642         fprintf(file, "typedef struct ");
643         printID(c->name);                                                       
644         fprintf(file, " {\n");
645         outputsize = 0;
646         dopadding = true;
647
648         printfields(c);
649
650         fprintf(file, "} ");
651         printID(c->name);
652         fprintf(file, ";\n\n");
653
654         /* create chain for renaming overloaded methods */
655         chain_free(ident_chain);
656         ident_chain = chain_new();
657
658         /* create method-prototypes */
659                                 
660         /* find overloaded methods */
661         for (i = 0; i < c->methodscount; i++) {
662
663                 m = &(c->methods[i]);
664
665                 if (!(m->flags & ACC_NATIVE)) continue;
666                 if (!m->nativelyoverloaded) {
667                         nativelyoverloaded=false;
668                         for (i2=i+1;i2<c->methodscount; i2++) {
669                                 m2 = &(c->methods[i2]);
670                                 if (!(m2->flags & ACC_NATIVE)) continue;
671                                 if (m->name==m2->name) {
672                                         m2->nativelyoverloaded=true;
673                                         nativelyoverloaded=true;
674                                 }
675                         }
676                         m->nativelyoverloaded=nativelyoverloaded;
677                 }
678
679         }
680
681         for (i = 0; i < c->methodscount; i++) {
682
683                 m = &(c->methods[i]);
684
685                 if (m->flags & ACC_NATIVE) {
686                         chain_addlast(nativemethod_chain, m);
687                         printmethod(m);
688                 }
689         }
690
691         chain_free(ident_chain);
692
693         fprintf(file, "#endif\n\n");
694
695         fclose(file);
696 }
697
698
699 /******** print classname, '$' used to seperate inner-class name ***********/
700
701 void print_classname(classinfo *clazz)
702 {
703         utf *u = clazz->name;
704     char *endpos  = u->text + u->blength;
705     char *utf_ptr = u->text; 
706         u2 c;
707
708     while (utf_ptr < endpos) {
709                 if ((c = utf_nextu2(&utf_ptr)) == '_')
710                         putc('$', file);
711                 else
712                         putc(c, file);
713         }
714
715
716
717 /*
718  * These are local overrides for various environment variables in Emacs.
719  * Please do not remove this and leave it at the end of the file, where
720  * Emacs will automagically detect them.
721  * ---------------------------------------------------------------------
722  * Local variables:
723  * mode: c
724  * indent-tabs-mode: t
725  * c-basic-offset: 4
726  * tab-width: 4
727  * End:
728  */