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_checkclasscast(java_objectheader *o, classinfo *c)
61 s4 new_builtin_checkintercast(java_objectheader *o, classinfo *c)
63 s4 new_builtin_checkarraycast
64 (java_objectheader *o, constant_arraydescriptor *d)
67 void new_builtin_aastore (java_objectarray *a, s4 index, java_objectheader *o) {}
69 u1 *createcompilerstub (methodinfo *m) {return NULL;}
70 u1 *createnativestub (functionptr f, methodinfo *m) {return NULL;}
71 u1 *ncreatenativestub (functionptr f, methodinfo *m) {return NULL;}
73 void removecompilerstub (u1 *stub) {}
74 void removenativestub (u1 *stub) {}
76 void perform_alpha_threadswitch (u1 **from, u1 **to) {}
77 u1* initialize_thread_stack (void *func, u1 *stack) { return NULL; }
78 void asm_switchstackandcall () { }
80 java_objectheader *native_new_and_init (void *p) { return NULL; }
82 /************************ globale Variablen **********************/
84 java_objectheader *exceptionptr; /* schani */
85 int newcompiler = true;
88 static chain *nativechain;
89 static FILE *file = NULL;
91 static void printIDpart (int c)
93 if ( (c>='a' && c<='z')
102 static void printID (unicode *name)
105 for (i=0; i<name->length; i++) {
106 printIDpart (name->text[i]);
114 static void addoutputsize (int len)
117 if (!dopadding) return;
119 newsize = ALIGN (outputsize, len);
121 for (i=outputsize; i<newsize; i++) fprintf (file, " u1 pad%d\n",(int) i);
122 outputsize = newsize;
126 static u2 *printtype (u2 *desc)
131 case 'V': fprintf (file, "void");
137 case 'Z': addoutputsize (4);
138 fprintf (file, "s4");
140 case 'J': addoutputsize (8);
141 fprintf (file, "s8");
143 case 'F': addoutputsize (4);
144 fprintf (file, "float");
146 case 'D': addoutputsize (8);
147 fprintf (file, "double");
150 addoutputsize ( sizeof(java_arrayheader*) );
152 case 'I': fprintf (file, "java_intarray*"); break;
153 case 'J': fprintf (file, "java_longarray*"); break;
154 case 'Z': fprintf (file, "java_booleanarray*"); break;
155 case 'B': fprintf (file, "java_bytearray*"); break;
156 case 'S': fprintf (file, "java_shortarray*"); break;
157 case 'C': fprintf (file, "java_chararray*"); break;
158 case 'F': fprintf (file, "java_floatarray*"); break;
159 case 'D': fprintf (file, "java_doublearray*"); break;
161 case '[': fprintf (file, "java_arrayarray*");
162 while ((*desc) == '[') desc++;
163 if ((*desc)!='L') desc++;
164 else while (*(desc++) != ';');
167 case 'L': fprintf (file, "java_objectarray*");
168 while ( *(desc++) != ';');
170 default: panic ("invalid type descriptor");
175 addoutputsize ( sizeof(java_objectheader*));
176 fprintf (file, "struct ");
177 while ( (c = *(desc++)) != ';' ) printIDpart (c);
181 default: panic ("Unknown type in field descriptor");
189 static void printfields (classinfo *c)
195 addoutputsize ( sizeof(java_objectheader) );
196 fprintf (file, " java_objectheader header;\n");
200 printfields (c->super);
202 for (i=0; i<c->fieldscount; i++) {
205 if (! (f->flags & ACC_STATIC) ) {
207 printtype (f->descriptor->text);
209 unicode_fprint (file, f->name);
210 fprintf (file, ";\n");
218 static void remembermethods (classinfo *c)
223 for (i=0; i<c->methodscount; i++) {
224 m = &(c->methods[i]);
226 if (m->flags & ACC_NATIVE) {
227 chain_addlast (nativechain, m);
236 static void printmethod (methodinfo *m)
241 d = m->descriptor->text;
242 while (*(d++) != ')');
246 printID (m->class->name);
251 d = m->descriptor->text+1;
253 if (! (m->flags & ACC_STATIC) ) {
254 fprintf (file, "struct ");
255 printID (m->class->name);
256 fprintf (file, "* this");
257 if ((*d)!=')') fprintf (file, ", ");
262 fprintf (file, " par%d", paramnum++);
263 if ((*d)!=')') fprintf (file, ", ");
266 fprintf (file, ");\n");
270 static void headers_generate (classinfo *c)
272 fprintf (file, "/* Structure information for class: ");
273 unicode_fprint (file, c->name);
274 fprintf (file, " */\n\n");
276 fprintf (file, "typedef struct ");
278 fprintf (file, " {\n");
284 fprintf (file, "} ");
286 fprintf (file, ";\n\n");
291 fprintf (file, "\n\n");
296 static void printnativetableentry (methodinfo *m)
298 fprintf (file, " { \"");
299 unicode_fprint (file, m->class->name);
300 fprintf (file, "\",\n \"");
301 unicode_fprint (file, m->name);
302 fprintf (file, "\",\n \"");
303 unicode_fprint (file, m->descriptor);
304 fprintf (file, "\",\n ");
305 if ( (m->flags & ACC_STATIC) !=0) fprintf (file, "true");
306 else fprintf (file, "false");
307 fprintf (file, ",\n ");
308 fprintf (file, "(functionptr) ");
309 printID (m->class->name);
312 fprintf (file,"\n },\n");
319 static void headers_start ()
321 file = fopen ("nativetypes.hh", "w");
322 if (!file) panic ("Can not open file 'native.h' to store header information");
324 fprintf (file, "/* Headerfile for native methods: nativetypes.hh */\n");
325 fprintf (file, "/* This file is machine generated, don't edit it !*/\n\n");
327 nativechain = chain_new ();
331 static void headers_finish ()
335 fprintf (file, "\n/* Prototypes for native methods */\n\n");
337 m = chain_first (nativechain);
342 m = chain_next (nativechain);
346 file = fopen ("nativetable.hh", "w");
347 if (!file) panic ("Can not open file 'nativetable' to store native-link-table");
349 fprintf (file, "/* Table of native methods: nativetables.hh */\n");
350 fprintf (file, "/* This file is machine generated, don't edit it !*/\n\n");
352 while ( (m = chain_first (nativechain)) != NULL) {
353 chain_remove (nativechain);
355 printnativetableentry (m);
359 chain_free (nativechain);
367 /******************** interne Funktion: print_usage ************************
369 Gibt die richtige Aufrufsyntax des JAVA-Header-Generators auf stdout aus.
371 ***************************************************************************/
373 static void print_usage()
375 printf ("USAGE: jch class [class..]\n");
381 /************************** Funktion: main *******************************
384 Wird vom System zu Programstart aufgerufen (eh klar).
386 **************************************************************************/
388 int main(int argc, char **argv)
396 /********** interne (nur fuer main relevante Optionen) **************/
398 char classpath[500] = "";
399 u4 heapsize = 100000;
401 /*********** Optionen, damit wirklich nur headers generiert werden ***/
403 makeinitializations=false;
406 /************ Infos aus der Environment lesen ************************/
408 cp = getenv ("CLASSPATH");
410 strcpy (classpath + strlen(classpath), ":");
411 strcpy (classpath + strlen(classpath), cp);
420 /**************************** Programmstart *****************************/
423 log_text ("Java - header-generator started");
426 suck_init (classpath);
429 heap_init (heapsize, heapsize, &dummy);
433 /*********************** JAVA-Klassen laden ***************************/
438 for (a=1; a<argc; a++) {
440 for (i=strlen(cp)-1; i>=0; i--) { /* Punkte im Klassennamen */
441 if (cp[i]=='.') cp[i]='/'; /* auf slashes umbauen */
444 topclass = loader_load ( unicode_new_char (cp) );
446 headers_generate (topclass);
453 /************************ Freigeben aller Resourcen *******************/
457 unicode_close (NULL);
460 /* Endemeldung ausgeben und mit entsprechendem exit-Status terminieren */
462 log_text ("Java - header-generator stopped");