1 /* gennativetable.c - generate nativetable.h for native.c
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
8 This file is part of CACAO.
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.
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.
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
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Christian Thalinger
31 $Id: gennativetable.c 1677 2004-12-03 16:54:25Z twisti $
42 #include "cacaoh/headers.h"
44 #include "mm/memory.h"
46 #if defined(USE_THREADS)
47 # if defined(NATIVE_THREADS)
48 # include "threads/native/threads.h"
50 # include "threads/green/threads.h"
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"
61 int main(int argc, char **argv)
64 char classpath[500] = "";
69 bool nativelyoverloaded;
70 u4 heapmaxsize = 2 * 1024 * 1024;
71 u4 heapstartsize = 100 * 1024;
78 printf("Usage: gennativetable class [class...]\n");
82 cp = getenv("CLASSPATH");
84 strcpy(classpath + strlen(classpath), ":");
85 strcpy(classpath + strlen(classpath), cp);
88 /* initialize the garbage collector */
89 gc_init(heapmaxsize, heapstartsize);
95 #if defined(USE_THREADS)
96 #if defined(NATIVE_THREADS)
102 loader_init((u1 *) &dummy);
105 /*********************** Load JAVA classes **************************/
107 nativeclass_chain = chain_new();
108 nativemethod_chain = chain_new();
110 for (i = 1; i < argc; i++) {
113 /* convert classname */
114 for (j = strlen(cp) - 1; j >= 0; j--) {
116 case '.': cp[j] = '/';
118 case '_': cp[j] = '$';
122 c = class_new(utf_new_char(cp));
124 /* exceptions are catched with new_exception call */
128 chain_addlast(nativeclass_chain, c);
130 /* find overloaded methods */
131 for (j = 0; j < c->methodscount; j++) {
132 m = &(c->methods[j]);
134 if (!(m->flags & ACC_NATIVE))
137 if (!m->nativelyoverloaded) {
138 nativelyoverloaded = false;
140 for (k = j + 1; k < c->methodscount; k++) {
141 m2 = &(c->methods[k]);
143 if (!(m2->flags & ACC_NATIVE))
146 if (m->name == m2->name) {
147 m2->nativelyoverloaded = true;
148 nativelyoverloaded = true;
151 m->nativelyoverloaded = nativelyoverloaded;
155 for (j = 0; j < c->methodscount; j++) {
156 m = &(c->methods[j]);
158 if (m->flags & ACC_NATIVE) {
159 chain_addlast(nativemethod_chain, m);
164 /* create table of native-methods */
168 fprintf(file, "/* Table of native methods: nativetables.inc */\n");
169 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
171 fprintf(file, "#include \"config.h\"\n");
173 c = chain_first(nativeclass_chain);
175 gen_header_filename(classname, c->name);
176 fprintf(file, "#include \"native/include/%s.h\"\n", classname);
177 c = chain_next(nativeclass_chain);
179 chain_free(nativeclass_chain);
181 fprintf(file, "\n\n#include \"native/native.h\"\n\n");
182 fprintf(file, "#if defined(STATIC_CLASSPATH)\n\n");
183 fprintf(file, "static nativeref nativetable[] = {\n");
185 m = chain_first(nativemethod_chain);
187 printnativetableentry(m);
188 m = chain_next(nativemethod_chain);
190 chain_free(nativemethod_chain);
192 fprintf(file, "};\n");
193 fprintf(file, "\n#else\n\n");
194 fprintf(file, "/* Ensure that symbols for functions implemented within cacao are used and */\n");
195 fprintf(file, "/* exported to dlopen. */\n\n");
196 fprintf(file, "static functionptr dummynativetable[] = {\n");
201 implData = fopen("vm/implementednatives.data", "r");
205 throw_cacao_exception_exit(string_java_lang_InternalError,
206 "Could not find file");
209 while (!feof(implData)) {
210 char functionLine[1024];
211 functionLine[0] = '\0';
212 fgets(functionLine, 1024, implData);
214 if (strlen(functionLine) < 2)
217 if (functionLine[strlen(functionLine) - 1] != '\n') {
223 functionLine[strlen(functionLine) - 1] = ',';
224 fprintf(file,"\t(functionptr) %s\n", functionLine);
227 fprintf(file, "\t(functionptr) 0\n");
231 fprintf(file, "};\n");
232 fprintf(file, "\n#endif\n");
235 /* release all resources */
238 tables_close(literalstring_free);
240 /* everything is ok */
247 * These are local overrides for various environment variables in Emacs.
248 * Please do not remove this and leave it at the end of the file, where
249 * Emacs will automagically detect them.
250 * ---------------------------------------------------------------------
253 * indent-tabs-mode: t