1 /* headers.c *******************************************************************
3 Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
5 See file COPYRIGHT for information on usage and disclaimer of warranties
7 Dieser Modul ersetzt f"ur den Headerfile-Betrieb den Modul 'main',
8 und 'f"alscht' einige Verweise auf externe Module (damit nicht schon
9 alle Module des eigentlichen Programmes fertig sein m"ussen, was ja
10 unm"oglich w"are, da die Headerfile-Tabellen ja erst hier und jetzt
13 Dieser Modul ist ein ziemlich schneller Hack und dementsprechend
14 schlecht (nicht) kommentiert.
16 Authors: Reinhard Grafl EMAIL: cacao@complang.tuwien.ac.at
17 Changes: Mark Probst EMAIL: cacao@complang.tuwien.ac.at
19 Last Change: 1997/05/23
21 *******************************************************************************/
29 /******* verschiedene externe Funktionen "faelschen" (=durch Dummys ersetzen),
30 damit der Linker zufrieden ist *********/
32 functionptr native_findfunction
33 (unicode *cname, unicode *mname, unicode *desc, bool isstatic)
36 java_objectheader *literalstring_new (unicode *text)
39 java_objectheader *javastring_new (unicode *text) /* schani */
42 void synchronize_caches() { }
43 void asm_call_jit_compiler () { }
44 void asm_calljavamethod () { }
45 void asm_dumpregistersandcall () { }
47 s4 asm_builtin_idiv (s4 a, s4 b) {return 0;}
48 s4 asm_builtin_irem (s4 a, s4 b) {return 0;}
49 s8 asm_builtin_ldiv (s8 a, s8 b) {return 0;}
50 s8 asm_builtin_lrem (s8 a, s8 b) {return 0;}
53 void asm_builtin_monitorenter (java_objectheader *o) {}
54 void asm_builtin_monitorexit (java_objectheader *o) {}
56 s4 asm_builtin_checkarraycast
57 (java_objectheader *o, constant_arraydescriptor *d)
60 void asm_builtin_aastore (java_objectarray *a, s4 index, java_objectheader *o) {}
62 u1 *createcompilerstub (methodinfo *m) {return NULL;}
63 u1 *createnativestub (functionptr f, methodinfo *m) {return NULL;}
64 u1 *oldcreatenativestub (functionptr f, methodinfo *m) {return NULL;}
66 void removecompilerstub (u1 *stub) {}
67 void removenativestub (u1 *stub) {}
69 void perform_alpha_threadswitch (u1 **from, u1 **to) {}
70 u1* initialize_thread_stack (void *func, u1 *stack) { return NULL; }
71 void asm_switchstackandcall () { }
73 java_objectheader *native_new_and_init (void *p) { return NULL; }
75 /************************ globale Variablen **********************/
77 java_objectheader *exceptionptr; /* schani */
78 int newcompiler = true;
81 static chain *nativechain;
82 static FILE *file = NULL;
84 static void printIDpart (int c)
86 if ( (c>='a' && c<='z')
95 static void printID (unicode *name)
98 for (i=0; i<name->length; i++) {
99 printIDpart (name->text[i]);
107 static void addoutputsize (int len)
110 if (!dopadding) return;
112 newsize = ALIGN (outputsize, len);
114 for (i=outputsize; i<newsize; i++) fprintf (file, " u1 pad%d\n",(int) i);
115 outputsize = newsize;
119 static u2 *printtype (u2 *desc)
124 case 'V': fprintf (file, "void");
130 case 'Z': addoutputsize (4);
131 fprintf (file, "s4");
133 case 'J': addoutputsize (8);
134 fprintf (file, "s8");
136 case 'F': addoutputsize (4);
137 fprintf (file, "float");
139 case 'D': addoutputsize (8);
140 fprintf (file, "double");
143 addoutputsize ( sizeof(java_arrayheader*) );
145 case 'I': fprintf (file, "java_intarray*"); break;
146 case 'J': fprintf (file, "java_longarray*"); break;
147 case 'Z': fprintf (file, "java_booleanarray*"); break;
148 case 'B': fprintf (file, "java_bytearray*"); break;
149 case 'S': fprintf (file, "java_shortarray*"); break;
150 case 'C': fprintf (file, "java_chararray*"); break;
151 case 'F': fprintf (file, "java_floatarray*"); break;
152 case 'D': fprintf (file, "java_doublearray*"); break;
154 case '[': fprintf (file, "java_arrayarray*");
155 while ((*desc) == '[') desc++;
156 if ((*desc)!='L') desc++;
157 else while (*(desc++) != ';');
160 case 'L': fprintf (file, "java_objectarray*");
161 while ( *(desc++) != ';');
163 default: panic ("invalid type descriptor");
168 addoutputsize ( sizeof(java_objectheader*));
169 fprintf (file, "struct ");
170 while ( (c = *(desc++)) != ';' ) printIDpart (c);
174 default: panic ("Unknown type in field descriptor");
182 static void printfields (classinfo *c)
188 addoutputsize ( sizeof(java_objectheader) );
189 fprintf (file, " java_objectheader header;\n");
193 printfields (c->super);
195 for (i=0; i<c->fieldscount; i++) {
198 if (! (f->flags & ACC_STATIC) ) {
200 printtype (f->descriptor->text);
202 unicode_fprint (file, f->name);
203 fprintf (file, ";\n");
211 static void remembermethods (classinfo *c)
216 for (i=0; i<c->methodscount; i++) {
217 m = &(c->methods[i]);
219 if (m->flags & ACC_NATIVE) {
220 chain_addlast (nativechain, m);
229 static void printmethod (methodinfo *m)
234 d = m->descriptor->text;
235 while (*(d++) != ')');
239 printID (m->class->name);
244 d = m->descriptor->text+1;
246 if (! (m->flags & ACC_STATIC) ) {
247 fprintf (file, "struct ");
248 printID (m->class->name);
249 fprintf (file, "* this");
250 if ((*d)!=')') fprintf (file, ", ");
255 fprintf (file, " par%d", paramnum++);
256 if ((*d)!=')') fprintf (file, ", ");
259 fprintf (file, ");\n");
263 static void headers_generate (classinfo *c)
265 fprintf (file, "/* Structure information for class: ");
266 unicode_fprint (file, c->name);
267 fprintf (file, " */\n\n");
269 fprintf (file, "typedef struct ");
271 fprintf (file, " {\n");
277 fprintf (file, "} ");
279 fprintf (file, ";\n\n");
284 fprintf (file, "\n\n");
289 static void printnativetableentry (methodinfo *m)
291 fprintf (file, " { \"");
292 unicode_fprint (file, m->class->name);
293 fprintf (file, "\",\n \"");
294 unicode_fprint (file, m->name);
295 fprintf (file, "\",\n \"");
296 unicode_fprint (file, m->descriptor);
297 fprintf (file, "\",\n ");
298 if ( (m->flags & ACC_STATIC) !=0) fprintf (file, "true");
299 else fprintf (file, "false");
300 fprintf (file, ",\n ");
301 fprintf (file, "(functionptr) ");
302 printID (m->class->name);
305 fprintf (file,"\n },\n");
312 static void headers_start ()
314 file = fopen ("nativetypes.hh", "w");
315 if (!file) panic ("Can not open file 'native.h' to store header information");
317 fprintf (file, "/* Headerfile for native methods: nativetypes.hh */\n");
318 fprintf (file, "/* This file is machine generated, don't edit it !*/\n\n");
320 nativechain = chain_new ();
324 static void headers_finish ()
328 fprintf (file, "\n/* Prototypes for native methods */\n\n");
330 m = chain_first (nativechain);
335 m = chain_next (nativechain);
339 file = fopen ("nativetable.hh", "w");
340 if (!file) panic ("Can not open file 'nativetable' to store native-link-table");
342 fprintf (file, "/* Table of native methods: nativetables.hh */\n");
343 fprintf (file, "/* This file is machine generated, don't edit it !*/\n\n");
345 while ( (m = chain_first (nativechain)) != NULL) {
346 chain_remove (nativechain);
348 printnativetableentry (m);
352 chain_free (nativechain);
360 /******************** interne Funktion: print_usage ************************
362 Gibt die richtige Aufrufsyntax des JAVA-Header-Generators auf stdout aus.
364 ***************************************************************************/
366 static void print_usage()
368 printf ("USAGE: jch class [class..]\n");
374 /************************** Funktion: main *******************************
377 Wird vom System zu Programstart aufgerufen (eh klar).
379 **************************************************************************/
381 int main(int argc, char **argv)
389 /********** interne (nur fuer main relevante Optionen) **************/
391 char classpath[500] = "";
392 u4 heapsize = 100000;
394 /*********** Optionen, damit wirklich nur headers generiert werden ***/
396 makeinitializations=false;
399 /************ Infos aus der Environment lesen ************************/
401 cp = getenv ("CLASSPATH");
403 strcpy (classpath + strlen(classpath), ":");
404 strcpy (classpath + strlen(classpath), cp);
413 /**************************** Programmstart *****************************/
416 log_text ("Java - header-generator started");
418 file = fopen("sysdep/offsets.h", "w");
420 panic ("Can not open file 'sysdep/offsets.h' for write");
422 fprintf (file, "/* This file is machine generated, don't edit it !*/\n\n");
424 fprintf (file, "#define offobjvftbl %3d\n", (int) OFFSET(java_objectheader, vftbl));
425 fprintf (file, "#define offarraysize %3d\n", (int) OFFSET(java_arrayheader, size));
426 fprintf (file, "#define offobjarrdata %3d\n\n", (int) OFFSET(java_objectarray, data[0]));
427 fprintf (file, "#define offbaseval %3d\n", (int) OFFSET(vftbl, baseval));
428 fprintf (file, "#define offdiffval %3d\n", (int) OFFSET(vftbl, diffval));
432 suck_init (classpath);
435 heap_init (heapsize, heapsize, &dummy);
439 /*********************** JAVA-Klassen laden ***************************/
444 for (a=1; a<argc; a++) {
446 for (i=strlen(cp)-1; i>=0; i--) { /* Punkte im Klassennamen */
447 if (cp[i]=='.') cp[i]='/'; /* auf slashes umbauen */
450 topclass = loader_load ( unicode_new_char (cp) );
452 headers_generate (topclass);
459 /************************ Freigeben aller Resourcen *******************/
463 unicode_close (NULL);
466 /* Endemeldung ausgeben und mit entsprechendem exit-Status terminieren */
468 log_text ("Java - header-generator stopped");
477 * These are local overrides for various environment variables in Emacs.
478 * Please do not remove this and leave it at the end of the file, where
479 * Emacs will automagically detect them.
480 * ---------------------------------------------------------------------
483 * indent-tabs-mode: t