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 new_builtin_idiv (s4 a, s4 b) {return 0;}
49 s4 new_builtin_irem (s4 a, s4 b) {return 0;}
50 s8 new_builtin_ldiv (s8 a, s8 b) {return 0;}
51 s8 new_builtin_lrem (s8 a, s8 b) {return 0;}
54 void new_builtin_monitorenter (java_objectheader *o) {}
55 void new_builtin_monitorexit (java_objectheader *o) {}
57 s4 new_builtin_checkcast(java_objectheader *o, classinfo *c)
59 s4 new_builtin_checkarraycast
60 (java_objectheader *o, constant_arraydescriptor *d)
63 void new_builtin_aastore (java_objectarray *a, s4 index, java_objectheader *o) {}
65 u1 *createcompilerstub (methodinfo *m) {return NULL;}
66 u1 *createnativestub (functionptr f, methodinfo *m) {return NULL;}
67 u1 *ncreatenativestub (functionptr f, methodinfo *m) {return NULL;}
69 void removecompilerstub (u1 *stub) {}
70 void removenativestub (u1 *stub) {}
72 void perform_alpha_threadswitch (u1 **from, u1 **to) {}
73 u1* initialize_thread_stack (void *func, u1 *stack) { return NULL; }
74 void asm_switchstackandcall () { }
76 java_objectheader *native_new_and_init (void *p) { return NULL; }
78 /************************ globale Variablen **********************/
80 java_objectheader *exceptionptr; /* schani */
81 int newcompiler = true;
84 static chain *nativechain;
85 static FILE *file = NULL;
87 static void printIDpart (int c)
89 if ( (c>='a' && c<='z')
98 static void printID (unicode *name)
101 for (i=0; i<name->length; i++) {
102 printIDpart (name->text[i]);
110 static void addoutputsize (int len)
113 if (!dopadding) return;
115 newsize = ALIGN (outputsize, len);
117 for (i=outputsize; i<newsize; i++) fprintf (file, " u1 pad%d\n",(int) i);
118 outputsize = newsize;
122 static u2 *printtype (u2 *desc)
127 case 'V': fprintf (file, "void");
133 case 'Z': addoutputsize (4);
134 fprintf (file, "s4");
136 case 'J': addoutputsize (8);
137 fprintf (file, "s8");
139 case 'F': addoutputsize (4);
140 fprintf (file, "float");
142 case 'D': addoutputsize (8);
143 fprintf (file, "double");
146 addoutputsize ( sizeof(java_arrayheader*) );
148 case 'I': fprintf (file, "java_intarray*"); break;
149 case 'J': fprintf (file, "java_longarray*"); break;
150 case 'Z': fprintf (file, "java_booleanarray*"); break;
151 case 'B': fprintf (file, "java_bytearray*"); break;
152 case 'S': fprintf (file, "java_shortarray*"); break;
153 case 'C': fprintf (file, "java_chararray*"); break;
154 case 'F': fprintf (file, "java_floatarray*"); break;
155 case 'D': fprintf (file, "java_doublearray*"); break;
157 case '[': fprintf (file, "java_arrayarray*");
158 while ((*desc) == '[') desc++;
159 if ((*desc)!='L') desc++;
160 else while (*(desc++) != ';');
163 case 'L': fprintf (file, "java_objectarray*");
164 while ( *(desc++) != ';');
166 default: panic ("invalid type descriptor");
171 addoutputsize ( sizeof(java_objectheader*));
172 fprintf (file, "struct ");
173 while ( (c = *(desc++)) != ';' ) printIDpart (c);
177 default: panic ("Unknown type in field descriptor");
185 static void printfields (classinfo *c)
191 addoutputsize ( sizeof(java_objectheader) );
192 fprintf (file, " java_objectheader header;\n");
196 printfields (c->super);
198 for (i=0; i<c->fieldscount; i++) {
201 if (! (f->flags & ACC_STATIC) ) {
203 printtype (f->descriptor->text);
205 unicode_fprint (file, f->name);
206 fprintf (file, ";\n");
214 static void remembermethods (classinfo *c)
219 for (i=0; i<c->methodscount; i++) {
220 m = &(c->methods[i]);
222 if (m->flags & ACC_NATIVE) {
223 chain_addlast (nativechain, m);
232 static void printmethod (methodinfo *m)
237 d = m->descriptor->text;
238 while (*(d++) != ')');
242 printID (m->class->name);
247 d = m->descriptor->text+1;
249 if (! (m->flags & ACC_STATIC) ) {
250 fprintf (file, "struct ");
251 printID (m->class->name);
252 fprintf (file, "* this");
253 if ((*d)!=')') fprintf (file, ", ");
258 fprintf (file, " par%d", paramnum++);
259 if ((*d)!=')') fprintf (file, ", ");
262 fprintf (file, ");\n");
266 static void headers_generate (classinfo *c)
268 fprintf (file, "/* Structure information for class: ");
269 unicode_fprint (file, c->name);
270 fprintf (file, " */\n\n");
272 fprintf (file, "typedef struct ");
274 fprintf (file, " {\n");
280 fprintf (file, "} ");
282 fprintf (file, ";\n\n");
287 fprintf (file, "\n\n");
292 static void printnativetableentry (methodinfo *m)
294 fprintf (file, " { \"");
295 unicode_fprint (file, m->class->name);
296 fprintf (file, "\",\n \"");
297 unicode_fprint (file, m->name);
298 fprintf (file, "\",\n \"");
299 unicode_fprint (file, m->descriptor);
300 fprintf (file, "\",\n ");
301 if ( (m->flags & ACC_STATIC) !=0) fprintf (file, "true");
302 else fprintf (file, "false");
303 fprintf (file, ",\n ");
304 fprintf (file, "(functionptr) ");
305 printID (m->class->name);
308 fprintf (file,"\n },\n");
315 static void headers_start ()
317 file = fopen ("nativetypes.hh", "w");
318 if (!file) panic ("Can not open file 'native.h' to store header information");
320 fprintf (file, "/* Headerfile for native methods: nativetypes.hh */\n");
321 fprintf (file, "/* This file is machine generated, don't edit it !*/\n\n");
323 nativechain = chain_new ();
327 static void headers_finish ()
331 fprintf (file, "\n/* Prototypes for native methods */\n\n");
333 m = chain_first (nativechain);
338 m = chain_next (nativechain);
342 file = fopen ("nativetable.hh", "w");
343 if (!file) panic ("Can not open file 'nativetable' to store native-link-table");
345 fprintf (file, "/* Table of native methods: nativetables.hh */\n");
346 fprintf (file, "/* This file is machine generated, don't edit it !*/\n\n");
348 while ( (m = chain_first (nativechain)) != NULL) {
349 chain_remove (nativechain);
351 printnativetableentry (m);
355 chain_free (nativechain);
363 /******************** interne Funktion: print_usage ************************
365 Gibt die richtige Aufrufsyntax des JAVA-Header-Generators auf stdout aus.
367 ***************************************************************************/
369 static void print_usage()
371 printf ("USAGE: jch class [class..]\n");
377 /************************** Funktion: main *******************************
380 Wird vom System zu Programstart aufgerufen (eh klar).
382 **************************************************************************/
384 int main(int argc, char **argv)
392 /********** interne (nur fuer main relevante Optionen) **************/
394 char classpath[500] = "";
395 u4 heapsize = 100000;
397 /*********** Optionen, damit wirklich nur headers generiert werden ***/
399 makeinitializations=false;
402 /************ Infos aus der Environment lesen ************************/
404 cp = getenv ("CLASSPATH");
406 strcpy (classpath + strlen(classpath), ":");
407 strcpy (classpath + strlen(classpath), cp);
416 /**************************** Programmstart *****************************/
419 log_text ("Java - header-generator started");
422 suck_init (classpath);
425 heap_init (heapsize, heapsize, &dummy);
429 /*********************** JAVA-Klassen laden ***************************/
434 for (a=1; a<argc; a++) {
436 for (i=strlen(cp)-1; i>=0; i--) { /* Punkte im Klassennamen */
437 if (cp[i]=='.') cp[i]='/'; /* auf slashes umbauen */
440 topclass = loader_load ( unicode_new_char (cp) );
442 headers_generate (topclass);
449 /************************ Freigeben aller Resourcen *******************/
453 unicode_close (NULL);
456 /* Endemeldung ausgeben und mit entsprechendem exit-Status terminieren */
458 log_text ("Java - header-generator stopped");