Removed : from classpath.
[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 1832 2004-12-29 13:48:07Z 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         loader_init((u1 *) &dummy);
102
103
104         /*********************** Load JAVA classes  **************************/
105
106         nativeclass_chain = chain_new();
107         nativemethod_chain = chain_new();
108
109         for (i = 1; i < argc; i++) {
110                 cp = argv[i];
111
112                 /* convert classname */
113                 for (j = strlen(cp) - 1; j >= 0; j--) {
114                         switch (cp[j]) {
115                         case '.': cp[j] = '/';
116                                 break;
117                         case '_': cp[j] = '$';
118                         }
119                 }
120         
121                 c = class_new(utf_new_char(cp));
122
123                 /* exceptions are catched with new_exception call */
124                 class_load(c);
125                 class_link(c);
126
127                 chain_addlast(nativeclass_chain, c);
128
129                 /* find overloaded methods */
130                 for (j = 0; j < c->methodscount; j++) {
131                         m = &(c->methods[j]);
132
133                         if (!(m->flags & ACC_NATIVE))
134                                 continue;
135
136                         if (!m->nativelyoverloaded) {
137                                 nativelyoverloaded = false;
138                                 
139                                 for (k = j + 1; k < c->methodscount; k++) {
140                                         m2 = &(c->methods[k]);
141
142                                         if (!(m2->flags & ACC_NATIVE))
143                                                 continue;
144
145                                         if (m->name == m2->name) {
146                                                 m2->nativelyoverloaded = true;
147                                                 nativelyoverloaded = true;
148                                         }
149                                 }
150                                 m->nativelyoverloaded = nativelyoverloaded;
151                         }
152                 }
153
154                 for (j = 0; j < c->methodscount; j++) {
155                         m = &(c->methods[j]);
156
157                         if (m->flags & ACC_NATIVE) {
158                                 chain_addlast(nativemethod_chain, m);
159                         }
160                 }
161         }
162
163         /* create table of native-methods */
164
165         file = stdout;
166
167         fprintf(file, "/* Table of native methods: nativetables.inc */\n");
168         fprintf(file, "/* This file is machine generated, don't edit it! */\n\n"); 
169
170         fprintf(file, "#include \"config.h\"\n");
171
172         c = chain_first(nativeclass_chain);
173         while (c) {
174                 gen_header_filename(classname, c->name);
175                 fprintf(file, "#include \"native/include/%s.h\"\n", classname);
176                 c = chain_next(nativeclass_chain);
177         }
178         chain_free(nativeclass_chain);
179
180         fprintf(file, "\n\n#include \"native/native.h\"\n\n");
181         fprintf(file, "#if defined(STATIC_CLASSPATH)\n\n");
182         fprintf(file, "static nativeref nativetable[] = {\n");
183
184         m = chain_first(nativemethod_chain);
185         while (m) {
186                 printnativetableentry(m);
187                 m = chain_next(nativemethod_chain);
188         }
189         chain_free(nativemethod_chain);
190
191         fprintf(file, "};\n");
192         fprintf(file, "\n#else\n\n");
193         fprintf(file, "/* Ensure that symbols for functions implemented within cacao are used and    */\n");
194         fprintf(file, "/* exported to dlopen.                                                        */\n\n");
195         fprintf(file, "static functionptr dummynativetable[] = {\n");
196
197         {
198                 FILE *implData;
199
200                 implData = fopen("vm/implementednatives.data", "r");
201
202                 if (!implData) {
203                         fclose(file);
204                         throw_cacao_exception_exit(string_java_lang_InternalError,
205                                                                            "Could not find file");
206                 }
207
208                 while (!feof(implData)) {
209                         char functionLine[1024];
210                         functionLine[0] = '\0';
211                         fgets(functionLine, 1024, implData);
212
213                         if (strlen(functionLine) < 2)
214                                 continue;
215
216                         if (functionLine[strlen(functionLine) - 1] != '\n') {
217                                 fclose(implData);
218                                 fclose(file);
219                                 exit(4);
220                         }
221
222                         functionLine[strlen(functionLine) - 1] = ',';
223                         fprintf(file,"\t(functionptr) %s\n", functionLine);
224                 }
225
226                 fprintf(file, "\t(functionptr) 0\n");
227                 fclose(implData);
228         }
229
230         fprintf(file, "};\n");
231         fprintf(file, "\n#endif\n");
232         fclose(file);
233         
234         /* release all resources */
235
236         loader_close();
237         tables_close();
238
239         /* everything is ok */
240
241         return 0;
242 }
243
244
245 /*
246  * These are local overrides for various environment variables in Emacs.
247  * Please do not remove this and leave it at the end of the file, where
248  * Emacs will automagically detect them.
249  * ---------------------------------------------------------------------
250  * Local variables:
251  * mode: c
252  * indent-tabs-mode: t
253  * c-basic-offset: 4
254  * tab-width: 4
255  * End:
256  */