Call new init code.
[cacao.git] / src / native / tools / gennativetable.c
1 /* gennativetable.c - generate nativetable.h for native.c
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: Christian Thalinger
28
29    Changes:
30
31    $Id: gennativetable.c 1916 2005-02-10 10:04:29Z twisti $
32
33 */
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include "config.h"
41 #include "types.h"
42 #include "cacaoh/headers.h"
43 #include "mm/boehm.h"
44 #include "mm/memory.h"
45
46 #if defined(USE_THREADS)
47 # if defined(NATIVE_THREADS)
48 #  include "threads/native/threads.h"
49 # else
50 #  include "threads/green/threads.h"
51 # endif
52 #endif
53
54 #include "toolbox/chain.h"
55 #include "vm/exceptions.h"
56 #include "vm/global.h"
57 #include "vm/loader.h"
58 #include "vm/tables.h"
59
60
61 int main(int argc, char **argv)
62 {
63         s4 i, j, k;
64         char classpath[500] = "";
65         char *cp;
66         classinfo *c;
67         methodinfo *m;
68         methodinfo *m2;
69         bool nativelyoverloaded;
70         u4 heapmaxsize = 2 * 1024 * 1024;
71         u4 heapstartsize = 100 * 1024;
72         void *dummy;
73
74         /* XXX change me */
75         char classname[1024];
76
77         if (argc < 2) {
78                 printf("Usage: gennativetable class [class...]\n");
79                 exit(1);
80         }
81
82         cp = getenv("CLASSPATH");
83         if (cp) {
84                 strcpy(classpath + strlen(classpath), cp);
85         }
86
87         /* initialize the garbage collector */
88         gc_init(heapmaxsize, heapstartsize);
89
90         tables_init();
91
92         suck_init(classpath);
93    
94 #if defined(USE_THREADS)
95 #if defined(NATIVE_THREADS)
96         initThreadsEarly();
97 #endif
98         initLocks();
99 #endif
100
101         /* initialize some cacao subsystems */
102
103         utf8_init();
104         class_init_foo();
105         loader_init((u1 *) &dummy);
106
107
108         /*********************** Load JAVA classes  **************************/
109
110         nativeclass_chain = chain_new();
111         nativemethod_chain = chain_new();
112
113         for (i = 1; i < argc; i++) {
114                 cp = argv[i];
115
116                 /* convert classname */
117                 for (j = strlen(cp) - 1; j >= 0; j--) {
118                         switch (cp[j]) {
119                         case '.': cp[j] = '/';
120                                 break;
121                         case '_': cp[j] = '$';
122                         }
123                 }
124         
125                 c = class_new(utf_new_char(cp));
126
127                 /* exceptions are catched with new_exception call */
128                 class_load(c);
129                 class_link(c);
130
131                 chain_addlast(nativeclass_chain, c);
132
133                 /* find overloaded methods */
134                 for (j = 0; j < c->methodscount; j++) {
135                         m = &(c->methods[j]);
136
137                         if (!(m->flags & ACC_NATIVE))
138                                 continue;
139
140                         if (!m->nativelyoverloaded) {
141                                 nativelyoverloaded = false;
142                                 
143                                 for (k = j + 1; k < c->methodscount; k++) {
144                                         m2 = &(c->methods[k]);
145
146                                         if (!(m2->flags & ACC_NATIVE))
147                                                 continue;
148
149                                         if (m->name == m2->name) {
150                                                 m2->nativelyoverloaded = true;
151                                                 nativelyoverloaded = true;
152                                         }
153                                 }
154                                 m->nativelyoverloaded = nativelyoverloaded;
155                         }
156                 }
157
158                 for (j = 0; j < c->methodscount; j++) {
159                         m = &(c->methods[j]);
160
161                         if (m->flags & ACC_NATIVE) {
162                                 chain_addlast(nativemethod_chain, m);
163                         }
164                 }
165         }
166
167         /* create table of native-methods */
168
169         file = stdout;
170
171         fprintf(file, "/* Table of native methods: nativetables.inc */\n");
172         fprintf(file, "/* This file is machine generated, don't edit it! */\n\n"); 
173
174         fprintf(file, "#include \"config.h\"\n");
175
176         c = chain_first(nativeclass_chain);
177         while (c) {
178                 gen_header_filename(classname, c->name);
179                 fprintf(file, "#include \"native/include/%s.h\"\n", classname);
180                 c = chain_next(nativeclass_chain);
181         }
182         chain_free(nativeclass_chain);
183
184         fprintf(file, "\n\n#include \"native/native.h\"\n\n");
185         fprintf(file, "#if defined(STATIC_CLASSPATH)\n\n");
186         fprintf(file, "static nativeref nativetable[] = {\n");
187
188         m = chain_first(nativemethod_chain);
189         while (m) {
190                 printnativetableentry(m);
191                 m = chain_next(nativemethod_chain);
192         }
193         chain_free(nativemethod_chain);
194
195         fprintf(file, "};\n");
196         fprintf(file, "\n#else\n\n");
197         fprintf(file, "/* Ensure that symbols for functions implemented within cacao are used and    */\n");
198         fprintf(file, "/* exported to dlopen.                                                        */\n\n");
199         fprintf(file, "static functionptr dummynativetable[] = {\n");
200
201         {
202                 FILE *implData;
203
204                 implData = fopen("vm/implementednatives.data", "r");
205
206                 if (!implData) {
207                         fclose(file);
208                         throw_cacao_exception_exit(string_java_lang_InternalError,
209                                                                            "Could not find file");
210                 }
211
212                 while (!feof(implData)) {
213                         char functionLine[1024];
214                         functionLine[0] = '\0';
215                         fgets(functionLine, 1024, implData);
216
217                         if (strlen(functionLine) < 2)
218                                 continue;
219
220                         if (functionLine[strlen(functionLine) - 1] != '\n') {
221                                 fclose(implData);
222                                 fclose(file);
223                                 exit(4);
224                         }
225
226                         functionLine[strlen(functionLine) - 1] = ',';
227                         fprintf(file,"\t(functionptr) %s\n", functionLine);
228                 }
229
230                 fprintf(file, "\t(functionptr) 0\n");
231                 fclose(implData);
232         }
233
234         fprintf(file, "};\n");
235         fprintf(file, "\n#endif\n");
236         fclose(file);
237         
238         /* release all resources */
239
240         loader_close();
241         tables_close();
242
243         /* everything is ok */
244
245         return 0;
246 }
247
248
249 /*
250  * These are local overrides for various environment variables in Emacs.
251  * Please do not remove this and leave it at the end of the file, where
252  * Emacs will automagically detect them.
253  * ---------------------------------------------------------------------
254  * Local variables:
255  * mode: c
256  * indent-tabs-mode: t
257  * c-basic-offset: 4
258  * tab-width: 4
259  * End:
260  */