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