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