1 /* gennativetable.c - generate nativetable.h for native.c
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
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 1832 2004-12-29 13:48:07Z 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), cp);
87 /* initialize the garbage collector */
88 gc_init(heapmaxsize, heapstartsize);
94 #if defined(USE_THREADS)
95 #if defined(NATIVE_THREADS)
101 loader_init((u1 *) &dummy);
104 /*********************** Load JAVA classes **************************/
106 nativeclass_chain = chain_new();
107 nativemethod_chain = chain_new();
109 for (i = 1; i < argc; i++) {
112 /* convert classname */
113 for (j = strlen(cp) - 1; j >= 0; j--) {
115 case '.': cp[j] = '/';
117 case '_': cp[j] = '$';
121 c = class_new(utf_new_char(cp));
123 /* exceptions are catched with new_exception call */
127 chain_addlast(nativeclass_chain, c);
129 /* find overloaded methods */
130 for (j = 0; j < c->methodscount; j++) {
131 m = &(c->methods[j]);
133 if (!(m->flags & ACC_NATIVE))
136 if (!m->nativelyoverloaded) {
137 nativelyoverloaded = false;
139 for (k = j + 1; k < c->methodscount; k++) {
140 m2 = &(c->methods[k]);
142 if (!(m2->flags & ACC_NATIVE))
145 if (m->name == m2->name) {
146 m2->nativelyoverloaded = true;
147 nativelyoverloaded = true;
150 m->nativelyoverloaded = nativelyoverloaded;
154 for (j = 0; j < c->methodscount; j++) {
155 m = &(c->methods[j]);
157 if (m->flags & ACC_NATIVE) {
158 chain_addlast(nativemethod_chain, m);
163 /* create table of native-methods */
167 fprintf(file, "/* Table of native methods: nativetables.inc */\n");
168 fprintf(file, "/* This file is machine generated, don't edit it! */\n\n");
170 fprintf(file, "#include \"config.h\"\n");
172 c = chain_first(nativeclass_chain);
174 gen_header_filename(classname, c->name);
175 fprintf(file, "#include \"native/include/%s.h\"\n", classname);
176 c = chain_next(nativeclass_chain);
178 chain_free(nativeclass_chain);
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");
184 m = chain_first(nativemethod_chain);
186 printnativetableentry(m);
187 m = chain_next(nativemethod_chain);
189 chain_free(nativemethod_chain);
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");
200 implData = fopen("vm/implementednatives.data", "r");
204 throw_cacao_exception_exit(string_java_lang_InternalError,
205 "Could not find file");
208 while (!feof(implData)) {
209 char functionLine[1024];
210 functionLine[0] = '\0';
211 fgets(functionLine, 1024, implData);
213 if (strlen(functionLine) < 2)
216 if (functionLine[strlen(functionLine) - 1] != '\n') {
222 functionLine[strlen(functionLine) - 1] = ',';
223 fprintf(file,"\t(functionptr) %s\n", functionLine);
226 fprintf(file, "\t(functionptr) 0\n");
230 fprintf(file, "};\n");
231 fprintf(file, "\n#endif\n");
234 /* release all resources */
239 /* everything is ok */
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 * ---------------------------------------------------------------------
252 * indent-tabs-mode: t