fix // comment
[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 1429 2004-11-02 08:58:26Z 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/locks.h"
47 #include "threads/thread.h"
48 #include "toolbox/chain.h"
49 #include "toolbox/memory.h"
50
51
52 int main(int argc, char **argv)
53 {
54         s4 i, j, k;
55         char classpath[500] = "";
56         char *cp;
57         classinfo *c;
58         methodinfo *m;
59         methodinfo *m2;
60         bool nativelyoverloaded;
61         u4 heapmaxsize = 2 * 1024 * 1024;
62         u4 heapstartsize = 100 * 1024;
63         void *dummy;
64
65         /* XXX change me */
66         char classname[1024];
67
68         if (argc < 2) {
69                 printf("Usage: gennativetable class [class...]\n");
70                 exit(1);
71         }
72
73         cp = getenv("CLASSPATH");
74         if (cp) {
75                 strcpy(classpath + strlen(classpath), ":");
76                 strcpy(classpath + strlen(classpath), cp);
77         }
78
79         /* initialize the garbage collector */
80         gc_init(heapmaxsize, heapstartsize);
81
82         tables_init();
83
84         suck_init(classpath);
85    
86 #if defined(USE_THREADS)
87 #if defined(NATIVE_THREADS)
88         initThreadsEarly();
89 #endif
90         initLocks();
91 #endif
92
93         loader_init((u1 *) &dummy);
94
95
96         /*********************** Load JAVA classes  **************************/
97
98         nativeclass_chain = chain_new();
99         nativemethod_chain = chain_new();
100
101         for (i = 1; i < argc; i++) {
102                 cp = argv[i];
103
104                 /* convert classname */
105                 for (j = strlen(cp) - 1; j >= 0; j--) {
106                         switch (cp[j]) {
107                         case '.': cp[j] = '/';
108                                 break;
109                         case '_': cp[j] = '$';
110                         }
111                 }
112         
113                 c = class_new(utf_new_char(cp));
114
115                 /* exceptions are catched with new_exception call */
116                 class_load(c);
117                 class_link(c);
118
119                 chain_addlast(nativeclass_chain, c);
120
121                 /* find overloaded methods */
122                 for (j = 0; j < c->methodscount; j++) {
123                         m = &(c->methods[j]);
124
125                         if (!(m->flags & ACC_NATIVE))
126                                 continue;
127
128                         if (!m->nativelyoverloaded) {
129                                 nativelyoverloaded = false;
130                                 
131                                 for (k = j + 1; k < c->methodscount; k++) {
132                                         m2 = &(c->methods[k]);
133
134                                         if (!(m2->flags & ACC_NATIVE))
135                                                 continue;
136
137                                         if (m->name == m2->name) {
138                                                 m2->nativelyoverloaded = true;
139                                                 nativelyoverloaded = true;
140                                         }
141                                 }
142                                 m->nativelyoverloaded = nativelyoverloaded;
143                         }
144                 }
145
146                 for (j = 0; j < c->methodscount; j++) {
147                         m = &(c->methods[j]);
148
149                         if (m->flags & ACC_NATIVE) {
150                                 chain_addlast(nativemethod_chain, m);
151                         }
152                 }
153         }
154
155         /* create table of native-methods */
156
157 /*      file = fopen("nativetable.h", "w"); */
158         file = stdout;
159
160         if (!file)
161                 panic("Can not open file 'nativetable' to store native-link-table");
162
163         fprintf(file, "/* Table of native methods: nativetables.inc */\n");
164         fprintf(file, "/* This file is machine generated, don't edit it !*/\n\n"); 
165
166         fprintf(file, "#include \"config.h\"\n");
167
168         c = chain_first(nativeclass_chain);
169         while (c) {
170                 gen_header_filename(classname, c->name);
171                 fprintf(file, "#include \"nat/%s.h\"\n", classname);
172                 c = chain_next(nativeclass_chain);
173         }
174         chain_free(nativeclass_chain);
175
176         fprintf(file, "\n\n#include \"native.h\"\n\n");
177         fprintf(file, "#ifdef STATIC_CLASSPATH\n\n");
178         fprintf(file, "static nativeref nativetable[] = {\n");
179
180         m = chain_first(nativemethod_chain);
181         while (m) {
182                 printnativetableentry(m);
183                 m = chain_next(nativemethod_chain);
184         }
185         chain_free(nativemethod_chain);
186
187         fprintf(file, "};\n");
188         fprintf(file,"\n#else\n");
189         fprintf(file, "/*ensure that symbols for functions implemented within cacao are used and exported to dlopen*/\n");
190         fprintf(file, "static functionptr dummynativetable[]={\n");
191         {
192                 FILE *implData=fopen("nat/implementednatives.data","r");
193                 if (!implData) {fclose(file); exit(3);}
194                 while (!feof(implData)) {
195                         char functionLine[1024];
196                         functionLine[0]='\0';
197                         fgets(functionLine,1024,implData);
198                         if (strlen(functionLine)<2) continue;
199                         if (functionLine[strlen(functionLine)-1]!='\n') { fclose(implData); fclose(file); exit(4);}
200                         functionLine[strlen(functionLine)-1]=',';
201                         fprintf(file,"\t(functionptr) %s\n",functionLine);
202                 }
203                 fprintf(file,"\t(functionptr)0");
204                 fclose(implData);
205         }
206         fprintf(file, "};\n");
207         fprintf(file,"\n#endif\n");
208         fclose(file);
209         
210         /* release all resources */
211
212         loader_close();
213         tables_close(literalstring_free);
214
215         /* everything is ok */
216
217         return 0;
218 }
219
220
221 /*
222  * These are local overrides for various environment variables in Emacs.
223  * Please do not remove this and leave it at the end of the file, where
224  * Emacs will automagically detect them.
225  * ---------------------------------------------------------------------
226  * Local variables:
227  * mode: c
228  * indent-tabs-mode: t
229  * c-basic-offset: 4
230  * tab-width: 4
231  * End:
232  */
233
234