43973647335608e4c80f7f61d4e416099e40f1bc
[cacao.git] / src / native / vm / nativevm.cpp
1 /* src/native/vm/nativevm.cpp - Register native VM interface functions.
2
3    Copyright (C) 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <stdint.h>
29
30 #include "native/vm/nativevm.hpp"
31
32 #include "vm/class.h"
33 #include "vm/initialize.h"
34 #include "vm/method.h"
35 #include "vm/options.h"
36 #include "vm/os.hpp"
37
38 #if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
39 # include "mm/memory.h"
40
41 # include "native/native.hpp"
42
43 # include "native/vm/openjdk/hpi.hpp"
44
45 # include "vm/globals.hpp"
46 # include "vm/properties.hpp"
47 # include "vm/utf8.h"
48 # include "vm/vm.hpp"
49 #endif
50
51
52 /* nativevm_preinit ************************************************************
53
54    Pre-initialize the implementation specific native stuff.
55
56 *******************************************************************************/
57
58 void nativevm_preinit(void)
59 {
60         TRACESUBSYSTEMINITIALIZATION("nativevm_preinit");
61
62         /* Register native methods of all classes implemented. */
63
64 #if defined(ENABLE_JAVASE)
65 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
66
67         _Jv_gnu_classpath_VMStackWalker_init();
68         _Jv_gnu_classpath_VMSystemProperties_init();
69         _Jv_gnu_java_lang_VMCPStringBuilder_init();
70         _Jv_gnu_java_lang_management_VMClassLoadingMXBeanImpl_init();
71         _Jv_gnu_java_lang_management_VMMemoryMXBeanImpl_init();
72         _Jv_gnu_java_lang_management_VMRuntimeMXBeanImpl_init();
73         _Jv_gnu_java_lang_management_VMThreadMXBeanImpl_init();
74         _Jv_java_lang_VMClass_init();
75         _Jv_java_lang_VMClassLoader_init();
76         _Jv_java_lang_VMObject_init();
77         _Jv_java_lang_VMRuntime_init();
78         _Jv_java_lang_VMSystem_init();
79         _Jv_java_lang_VMString_init();
80         _Jv_java_lang_VMThread_init();
81         _Jv_java_lang_VMThrowable_init();
82         _Jv_java_lang_management_VMManagementFactory_init();
83         _Jv_java_lang_reflect_VMConstructor_init();
84         _Jv_java_lang_reflect_VMField_init();
85         _Jv_java_lang_reflect_VMMethod_init();
86         //_Jv_java_lang_reflect_VMProxy_init();
87         _Jv_java_security_VMAccessController_init();
88         _Jv_java_util_concurrent_atomic_AtomicLong_init();
89         _Jv_sun_misc_Unsafe_init();
90
91 #if defined(ENABLE_ANNOTATIONS)
92         _Jv_sun_reflect_ConstantPool_init();
93 #endif
94
95 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
96
97         // Load libjava.so
98         VM* vm = VM::get_current();
99         Properties& properties = vm->get_properties();
100         const char* boot_library_path = properties.get("sun.boot.library.path");
101
102         size_t len =
103                 os::strlen(boot_library_path) +
104                 os::strlen("/libjava.so") +
105                 os::strlen("0");
106
107         char* p = MNEW(char, len);
108
109         os::strcpy(p, boot_library_path);
110         os::strcat(p, "/libjava.so");
111
112         utf* u = utf_new_char(p);
113
114         NativeLibrary nl(u);
115         void* handle = nl.open();
116
117         if (handle == NULL)
118                 os::abort("nativevm_init: failed to open libjava.so at: %s", p);
119
120         MFREE(p, char, len);
121
122         NativeLibraries& nls = vm->get_nativelibraries();
123         nls.add(nl);
124
125         // Initialize the HPI.
126         HPI& hpi = vm->get_hpi();
127         hpi.initialize();
128
129         _Jv_sun_misc_Unsafe_init();
130
131 # else
132 #  error unknown classpath configuration
133 # endif
134
135 #elif defined(ENABLE_JAVAME_CLDC1_1)
136
137         _Jv_com_sun_cldc_io_ResourceInputStream_init();
138         _Jv_com_sun_cldc_io_j2me_socket_Protocol_init();
139         _Jv_com_sun_cldchi_io_ConsoleOutputStream_init();
140         _Jv_com_sun_cldchi_jvm_JVM_init();
141         _Jv_java_lang_Class_init();
142         _Jv_java_lang_Double_init();
143         _Jv_java_lang_Float_init();
144         _Jv_java_lang_Math_init();
145         _Jv_java_lang_Object_init();
146         _Jv_java_lang_Runtime_init();
147         _Jv_java_lang_String_init();
148         _Jv_java_lang_System_init();
149         _Jv_java_lang_Thread_init();
150         _Jv_java_lang_Throwable_init();
151
152 #else
153 # error unknown Java configuration
154 #endif
155 }
156
157
158 /* nativevm_init ***************************************************************
159
160    Initialize the implementation specific native stuff.
161
162 *******************************************************************************/
163
164 void nativevm_init(void)
165 {
166         TRACESUBSYSTEMINITIALIZATION("nativevm_init");
167
168 #if defined(ENABLE_JAVASE)
169
170 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
171
172         // Nothing to do.
173
174 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
175
176         methodinfo* m = class_resolveclassmethod(class_java_lang_System,
177                                                                                          utf_new_char("initializeSystemClass"),
178                                                                                          utf_void__void,
179                                                                                          class_java_lang_Object,
180                                                                                          false);
181
182         if (m == NULL)
183                 os::abort("nativevm_init: Error resolving java.lang.System.initializeSystemClass()");
184
185         (void) vm_call_method(m, NULL);
186
187 # else
188 #  error unknown classpath configuration
189 # endif
190
191 #elif defined(ENABLE_JAVAME_CLDC1_1)
192
193         // Nothing to do.
194
195 #else
196 # error unknown Java configuration
197 #endif
198 }
199
200
201 /*
202  * These are local overrides for various environment variables in Emacs.
203  * Please do not remove this and leave it at the end of the file, where
204  * Emacs will automagically detect them.
205  * ---------------------------------------------------------------------
206  * Local variables:
207  * mode: c++
208  * indent-tabs-mode: t
209  * c-basic-offset: 4
210  * tab-width: 4
211  * End:
212  * vim:noexpandtab:sw=4:ts=4:
213  */