1 /* -*- mode: c; tab-width: 4; c-basic-offset: 4 -*- */
2 /****************************** headers.c **************************************
4 Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
6 See file COPYRIGHT for information on usage and disclaimer of warranties
8 Dieser Modul ersetzt f"ur den Headerfile-Betrieb den Modul 'main',
9 und 'f"alscht' einige Verweise auf externe Module (damit nicht schon
10 alle Module des eigentlichen Programmes fertig sein m"ussen, was ja
11 unm"oglich w"are, da die Headerfile-Tabellen ja erst hier und jetzt
14 Dieser Modul ist ein ziemlich schneller Hack und dementsprechend
15 schlecht (nicht) kommentiert.
17 Authors: Reinhard Grafl EMAIL: cacao@complang.tuwien.ac.at
18 Changes: Mark Probst EMAIL: cacao@complang.tuwien.ac.at
20 Last Change: 1997/05/23
22 *******************************************************************************/
30 /******* verschiedene externe Funktionen "faelschen" (=durch Dummys ersetzen),
31 damit der Linker zufrieden ist *********/
33 functionptr native_findfunction
34 (unicode *cname, unicode *mname, unicode *desc, bool isstatic)
37 java_objectheader *literalstring_new (unicode *text)
40 java_objectheader *javastring_new (unicode *text) /* schani */
43 void synchronize_caches() { }
44 void asm_call_jit_compiler () { }
45 void asm_calljavamethod () { }
46 void asm_dumpregistersandcall () { }
48 s4 asm_builtin_idiv (s4 a, s4 b) {return 0;}
49 s4 asm_builtin_irem (s4 a, s4 b) {return 0;}
50 s8 asm_builtin_ldiv (s8 a, s8 b) {return 0;}
51 s8 asm_builtin_lrem (s8 a, s8 b) {return 0;}
54 void asm_builtin_monitorenter (java_objectheader *o) {}
55 void asm_builtin_monitorexit (java_objectheader *o) {}
57 s4 asm_builtin_checkarraycast
58 (java_objectheader *o, constant_arraydescriptor *d)
61 void asm_builtin_aastore (java_objectarray *a, s4 index, java_objectheader *o) {}
63 u1 *createcompilerstub (methodinfo *m) {return NULL;}
64 u1 *createnativestub (functionptr f, methodinfo *m) {return NULL;}
65 u1 *ncreatenativestub (functionptr f, methodinfo *m) {return NULL;}
67 void removecompilerstub (u1 *stub) {}
68 void removenativestub (u1 *stub) {}
70 void perform_alpha_threadswitch (u1 **from, u1 **to) {}
71 u1* initialize_thread_stack (void *func, u1 *stack) { return NULL; }
72 void asm_switchstackandcall () { }
74 java_objectheader *native_new_and_init (void *p) { return NULL; }
76 /************************ globale Variablen **********************/
78 java_objectheader *exceptionptr; /* schani */
79 int newcompiler = true;
82 static chain *nativechain;
83 static FILE *file = NULL;
85 static void printIDpart (int c)
87 if ( (c>='a' && c<='z')
96 static void printID (unicode *name)
99 for (i=0; i<name->length; i++) {
100 printIDpart (name->text[i]);
108 static void addoutputsize (int len)
111 if (!dopadding) return;
113 newsize = ALIGN (outputsize, len);
115 for (i=outputsize; i<newsize; i++) fprintf (file, " u1 pad%d\n",(int) i);
116 outputsize = newsize;
120 static u2 *printtype (u2 *desc)
125 case 'V': fprintf (file, "void");
131 case 'Z': addoutputsize (4);
132 fprintf (file, "s4");
134 case 'J': addoutputsize (8);
135 fprintf (file, "s8");
137 case 'F': addoutputsize (4);
138 fprintf (file, "float");
140 case 'D': addoutputsize (8);
141 fprintf (file, "double");
144 addoutputsize ( sizeof(java_arrayheader*) );
146 case 'I': fprintf (file, "java_intarray*"); break;
147 case 'J': fprintf (file, "java_longarray*"); break;
148 case 'Z': fprintf (file, "java_booleanarray*"); break;
149 case 'B': fprintf (file, "java_bytearray*"); break;
150 case 'S': fprintf (file, "java_shortarray*"); break;
151 case 'C': fprintf (file, "java_chararray*"); break;
152 case 'F': fprintf (file, "java_floatarray*"); break;
153 case 'D': fprintf (file, "java_doublearray*"); break;
155 case '[': fprintf (file, "java_arrayarray*");
156 while ((*desc) == '[') desc++;
157 if ((*desc)!='L') desc++;
158 else while (*(desc++) != ';');
161 case 'L': fprintf (file, "java_objectarray*");
162 while ( *(desc++) != ';');
164 default: panic ("invalid type descriptor");
169 addoutputsize ( sizeof(java_objectheader*));
170 fprintf (file, "struct ");
171 while ( (c = *(desc++)) != ';' ) printIDpart (c);
175 default: panic ("Unknown type in field descriptor");
183 static void printfields (classinfo *c)
189 addoutputsize ( sizeof(java_objectheader) );
190 fprintf (file, " java_objectheader header;\n");
194 printfields (c->super);
196 for (i=0; i<c->fieldscount; i++) {
199 if (! (f->flags & ACC_STATIC) ) {
201 printtype (f->descriptor->text);
203 unicode_fprint (file, f->name);
204 fprintf (file, ";\n");
212 static void remembermethods (classinfo *c)
217 for (i=0; i<c->methodscount; i++) {
218 m = &(c->methods[i]);
220 if (m->flags & ACC_NATIVE) {
221 chain_addlast (nativechain, m);
230 static void printmethod (methodinfo *m)
235 d = m->descriptor->text;
236 while (*(d++) != ')');
240 printID (m->class->name);
245 d = m->descriptor->text+1;
247 if (! (m->flags & ACC_STATIC) ) {
248 fprintf (file, "struct ");
249 printID (m->class->name);
250 fprintf (file, "* this");
251 if ((*d)!=')') fprintf (file, ", ");
256 fprintf (file, " par%d", paramnum++);
257 if ((*d)!=')') fprintf (file, ", ");
260 fprintf (file, ");\n");
264 static void headers_generate (classinfo *c)
266 fprintf (file, "/* Structure information for class: ");
267 unicode_fprint (file, c->name);
268 fprintf (file, " */\n\n");
270 fprintf (file, "typedef struct ");
272 fprintf (file, " {\n");
278 fprintf (file, "} ");
280 fprintf (file, ";\n\n");
285 fprintf (file, "\n\n");
290 static void printnativetableentry (methodinfo *m)
292 fprintf (file, " { \"");
293 unicode_fprint (file, m->class->name);
294 fprintf (file, "\",\n \"");
295 unicode_fprint (file, m->name);
296 fprintf (file, "\",\n \"");
297 unicode_fprint (file, m->descriptor);
298 fprintf (file, "\",\n ");
299 if ( (m->flags & ACC_STATIC) !=0) fprintf (file, "true");
300 else fprintf (file, "false");
301 fprintf (file, ",\n ");
302 fprintf (file, "(functionptr) ");
303 printID (m->class->name);
306 fprintf (file,"\n },\n");
313 static void headers_start ()
315 file = fopen ("nativetypes.hh", "w");
316 if (!file) panic ("Can not open file 'native.h' to store header information");
318 fprintf (file, "/* Headerfile for native methods: nativetypes.hh */\n");
319 fprintf (file, "/* This file is machine generated, don't edit it !*/\n\n");
321 nativechain = chain_new ();
325 static void headers_finish ()
329 fprintf (file, "\n/* Prototypes for native methods */\n\n");
331 m = chain_first (nativechain);
336 m = chain_next (nativechain);
340 file = fopen ("nativetable.hh", "w");
341 if (!file) panic ("Can not open file 'nativetable' to store native-link-table");
343 fprintf (file, "/* Table of native methods: nativetables.hh */\n");
344 fprintf (file, "/* This file is machine generated, don't edit it !*/\n\n");
346 while ( (m = chain_first (nativechain)) != NULL) {
347 chain_remove (nativechain);
349 printnativetableentry (m);
353 chain_free (nativechain);
361 /******************** interne Funktion: print_usage ************************
363 Gibt die richtige Aufrufsyntax des JAVA-Header-Generators auf stdout aus.
365 ***************************************************************************/
367 static void print_usage()
369 printf ("USAGE: jch class [class..]\n");
375 /************************** Funktion: main *******************************
378 Wird vom System zu Programstart aufgerufen (eh klar).
380 **************************************************************************/
382 int main(int argc, char **argv)
390 /********** interne (nur fuer main relevante Optionen) **************/
392 char classpath[500] = "";
393 u4 heapsize = 100000;
395 /*********** Optionen, damit wirklich nur headers generiert werden ***/
397 makeinitializations=false;
400 /************ Infos aus der Environment lesen ************************/
402 cp = getenv ("CLASSPATH");
404 strcpy (classpath + strlen(classpath), ":");
405 strcpy (classpath + strlen(classpath), cp);
414 /**************************** Programmstart *****************************/
417 log_text ("Java - header-generator started");
419 file = fopen("sysdep/offsets.h", "w");
421 panic ("Can not open file 'sysdep/offsets.h' for write");
423 fprintf (file, "/* This file is machine generated, don't edit it !*/\n\n");
425 fprintf (file, "#define offobjvftbl %3d\n", (int) OFFSET(java_objectheader, vftbl));
426 fprintf (file, "#define offarraysize %3d\n", (int) OFFSET(java_arrayheader, size));
427 fprintf (file, "#define offobjarrdata %3d\n\n", (int) OFFSET(java_objectarray, data[0]));
428 fprintf (file, "#define offbaseval %3d\n", (int) OFFSET(vftbl, baseval));
429 fprintf (file, "#define offdiffval %3d\n", (int) OFFSET(vftbl, diffval));
433 suck_init (classpath);
436 heap_init (heapsize, heapsize, &dummy);
440 /*********************** JAVA-Klassen laden ***************************/
445 for (a=1; a<argc; a++) {
447 for (i=strlen(cp)-1; i>=0; i--) { /* Punkte im Klassennamen */
448 if (cp[i]=='.') cp[i]='/'; /* auf slashes umbauen */
451 topclass = loader_load ( unicode_new_char (cp) );
453 headers_generate (topclass);
460 /************************ Freigeben aller Resourcen *******************/
464 unicode_close (NULL);
467 /* Endemeldung ausgeben und mit entsprechendem exit-Status terminieren */
469 log_text ("Java - header-generator stopped");