fix cacao/gennativetable to not crash on zipfile input, whole classpath in cacaodev...
[cacao.git] / gennativetable.c
1 /* gennativetable.c - generate nativetable.h for native.c
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5    M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6    P. Tomsich, J. Wenninger
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 1264 2004-07-01 14:21:05Z jowenn $
32
33 */
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include "config.h"
40 #include "types.h"
41 #include "global.h"
42 #include "headers.h"
43 #include "loader.h"
44 #include "tables.h"
45 #include "mm/boehm.h"
46 #include "threads/thread.h"
47 #include "toolbox/chain.h"
48 #include "toolbox/memory.h"
49
50
51 int main(int argc, char **argv)
52 {
53         s4 i, j, k;
54         char classpath[500] = "";
55         char *cp;
56         classinfo *c;
57         methodinfo *m;
58         methodinfo *m2;
59         bool nativelyoverloaded;
60         u4 heapmaxsize = 2 * 1024 * 1024;
61         u4 heapstartsize = 100 * 1024;
62         void *dummy;
63
64         /* XXX change me */
65         char classname[1024];
66
67         if (argc < 2) {
68                 printf("Usage: gennativetable class [class...]\n");
69                 exit(1);
70         }
71
72         cp = getenv("CLASSPATH");
73         if (cp) {
74                 strcpy(classpath + strlen(classpath), ":");
75                 strcpy(classpath + strlen(classpath), cp);
76         }
77
78         /* initialize the garbage collector */
79         gc_init(heapmaxsize, heapstartsize);
80
81         tables_init();
82
83         suck_init(classpath);
84    
85
86 #if defined(USE_THREADS) && defined(NATIVE_THREADS)
87         initThreadsEarly();
88 #endif
89         loader_init((u1 *) &dummy);
90
91
92         /*********************** Load JAVA classes  **************************/
93
94         nativeclass_chain = chain_new();
95         nativemethod_chain = chain_new();
96
97         for (i = 1; i < argc; i++) {
98                 cp = argv[i];
99
100                 /* convert classname */
101                 for (j = strlen(cp) - 1; j >= 0; j--) {
102                         switch (cp[j]) {
103                         case '.': cp[j] = '/';
104                                 break;
105                         case '_': cp[j] = '$';
106                         }
107                 }
108         
109                 c = class_new(utf_new_char(cp));
110
111                 /* exceptions are catched with new_exception call */
112                 class_load(c);
113                 class_link(c);
114
115                 chain_addlast(nativeclass_chain, c);
116
117                 /* find overloaded methods */
118                 for (j = 0; j < c->methodscount; j++) {
119                         m = &(c->methods[j]);
120
121                         if (!(m->flags & ACC_NATIVE))
122                                 continue;
123
124                         if (!m->nativelyoverloaded) {
125                                 nativelyoverloaded = false;
126                                 
127                                 for (k = j + 1; k < c->methodscount; k++) {
128                                         m2 = &(c->methods[k]);
129
130                                         if (!(m2->flags & ACC_NATIVE))
131                                                 continue;
132
133                                         if (m->name == m2->name) {
134                                                 m2->nativelyoverloaded = true;
135                                                 nativelyoverloaded = true;
136                                         }
137                                 }
138                                 m->nativelyoverloaded = nativelyoverloaded;
139                         }
140                 }
141
142                 for (j = 0; j < c->methodscount; j++) {
143                         m = &(c->methods[j]);
144
145                         if (m->flags & ACC_NATIVE) {
146                                 chain_addlast(nativemethod_chain, m);
147                         }
148                 }
149         }
150
151         /* create table of native-methods */
152
153 /*      file = fopen("nativetable.h", "w"); */
154         file = stdout;
155
156         if (!file)
157                 panic("Can not open file 'nativetable' to store native-link-table");
158
159         fprintf(file, "/* Table of native methods: nativetables.hh */\n");
160         fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n"); 
161
162         c = chain_first(nativeclass_chain);
163         while (c) {
164                 gen_header_filename(classname, c->name);
165                 fprintf(file, "#include \"nat/%s.h\"\n", classname);
166                 c = chain_next(nativeclass_chain);
167         }
168         chain_free(nativeclass_chain);
169
170         fprintf(file, "\n\n#include \"native.h\"\n\n");
171         fprintf(file, "static nativeref nativetable[] = {\n");
172
173         m = chain_first(nativemethod_chain);
174         while (m) {
175                 printnativetableentry(m);
176                 m = chain_next(nativemethod_chain);
177         }
178         chain_free(nativemethod_chain);
179
180         fprintf(file, "};\n");
181
182         fclose(file);
183
184         /* release all resources */
185
186         loader_close();
187         tables_close(literalstring_free);
188
189         /* everything is ok */
190
191         return 0;
192 }
193
194
195 /*
196  * These are local overrides for various environment variables in Emacs.
197  * Please do not remove this and leave it at the end of the file, where
198  * Emacs will automagically detect them.
199  * ---------------------------------------------------------------------
200  * Local variables:
201  * mode: c
202  * indent-tabs-mode: t
203  * c-basic-offset: 4
204  * tab-width: 4
205  * End:
206  */
207
208