f51b38fc8033334eb92379b407efcad81a4f2362
[cacao.git] / src / native / tools / gennativetable.c
1 /* src/native/tools/gennativetable.c - generate nativetable.h for native.c
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Christian Thalinger
28
29    Changes:
30
31    $Id: gennativetable.c 4357 2006-01-22 23:33:38Z twisti $
32
33 */
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include "config.h"
41 #include "vm/types.h"
42
43 #include "cacaoh/headers.h"
44 #include "mm/boehm.h"
45 #include "mm/memory.h"
46
47 #if defined(USE_THREADS)
48 # if defined(NATIVE_THREADS)
49 #  include "threads/native/threads.h"
50 # else
51 #  include "threads/green/threads.h"
52 # endif
53 #endif
54
55 #include "toolbox/chain.h"
56 #include "vm/classcache.h"
57 #include "vm/exceptions.h"
58 #include "vm/global.h"
59 #include "vm/loader.h"
60 #include "vm/tables.h"
61
62
63 int main(int argc, char **argv)
64 {
65         char *bootclasspath;
66         char *cp;
67
68         classcache_name_entry *nmen;
69         classcache_class_entry *clsen;
70         classinfo *c;
71         s4 i;
72         s4 j;
73         u4 slot;
74         methodinfo *m;
75         methodinfo *m2;
76         bool nativelyoverloaded;
77
78         u4 heapmaxsize = 4 * 1024 * 1024;
79         u4 heapstartsize = 100 * 1024;
80         void *dummy;
81
82         /* set the bootclasspath */
83
84         cp = getenv("BOOTCLASSPATH");
85         if (cp) {
86                 bootclasspath = MNEW(char, strlen(cp) + 1);
87                 strcpy(bootclasspath, cp);
88         }
89
90         /* initialize the garbage collector */
91         gc_init(heapmaxsize, heapstartsize);
92
93         tables_init();
94
95         suck_init(bootclasspath);
96    
97 #if defined(USE_THREADS)
98 #if defined(NATIVE_THREADS)
99         initThreadsEarly();
100 #endif
101         initLocks();
102 #endif
103
104         /* initialize some cacao subsystems */
105
106         utf8_init();
107         loader_init((u1 *) &dummy);
108
109
110         /*********************** Load JAVA classes  **************************/
111
112         nativemethod_chain = chain_new();
113         ident_chain = chain_new();
114
115         /* load all classes from bootclasspath */
116
117         loader_load_all_classes();
118
119         /* link all classes */
120
121         for (slot = 0; slot < classcache_hash.size; slot++) {
122                 nmen = (classcache_name_entry *) classcache_hash.ptr[slot];
123
124                 for (; nmen; nmen = nmen->hashlink) {
125                         /* iterate over all class entries */
126
127                         for (clsen = nmen->classes; clsen; clsen = clsen->next) {
128                                 c = clsen->classobj;
129
130                                 if (!c)
131                                         continue;
132
133                                 /* exceptions are catched with new_exception call */
134
135                                 if (!c->linked)
136                                         (void) link_class(c);
137
138                                 /* find overloaded methods */
139
140                                 for (i = 0; i < c->methodscount; i++) {
141                                         m = &(c->methods[i]);
142
143                                         if (!(m->flags & ACC_NATIVE))
144                                                 continue;
145
146                                         if (!m->nativelyoverloaded) {
147                                                 nativelyoverloaded = false;
148                                 
149                                                 for (j = i + 1; j < c->methodscount; j++) {
150                                                         m2 = &(c->methods[j]);
151
152                                                         if (!(m2->flags & ACC_NATIVE))
153                                                                 continue;
154
155                                                         if (m->name == m2->name) {
156                                                                 m2->nativelyoverloaded = true;
157                                                                 nativelyoverloaded = true;
158                                                         }
159                                                 }
160
161                                                 m->nativelyoverloaded = nativelyoverloaded;
162                                         }
163                                 }
164
165                                 for (j = 0; j < c->methodscount; j++) {
166                                         m = &(c->methods[j]);
167
168                                         if (m->flags & ACC_NATIVE) {
169                                                 chain_addlast(nativemethod_chain, m);
170                                         }
171                                 }
172                         }
173                 }
174         }
175
176         /* create table of native-methods */
177
178         file = stdout;
179
180         fprintf(file, "/* This file is machine generated, don't edit it! */\n\n"); 
181
182         m = chain_first(nativemethod_chain);
183
184         while (m) {
185                 printmethod(m);
186                 m = chain_next(nativemethod_chain);
187         }
188
189         fprintf(file, "static nativeref nativetable[] = {\n");
190
191         m = chain_first(nativemethod_chain);
192
193         while (m) {
194         fprintf(file, "   { \"");
195
196                 print_classname(m->class);
197                 fprintf(file, "\",\n     \"");
198                 utf_fprint(file, m->name);
199                 fprintf(file, "\",\n     \"");
200                 utf_fprint(file, m->descriptor);
201                 fprintf(file, "\",\n     ");
202
203                 if (m->flags & ACC_STATIC)
204                         fprintf(file, "true");
205                 else
206                         fprintf(file, "false");
207
208                 fprintf(file, ",\n     ");
209                 fprintf(file, "(functionptr) Java_");
210                 printID(m->class->name);
211                 fprintf(file, "_");
212                 printID(m->name);
213          
214                 if (m->nativelyoverloaded)
215                         printOverloadPart(m->descriptor);
216
217                 fprintf(file,"\n   },\n");
218
219                 m = chain_next(nativemethod_chain);
220         }
221
222         chain_free(nativemethod_chain);
223         chain_free(ident_chain);
224
225         fprintf(file, "};\n");
226
227         fclose(file);
228         
229         /* release all resources */
230
231         loader_close();
232
233         /* everything is ok */
234
235         return 0;
236 }
237
238
239 /*
240  * These are local overrides for various environment variables in Emacs.
241  * Please do not remove this and leave it at the end of the file, where
242  * Emacs will automagically detect them.
243  * ---------------------------------------------------------------------
244  * Local variables:
245  * mode: c
246  * indent-tabs-mode: t
247  * c-basic-offset: 4
248  * tab-width: 4
249  * End:
250  */