CACAO
org_cacaojvm_compiler2_test_Compiler2Test.cpp
Go to the documentation of this file.
1 /* src/native/compiler2/org_cacaojvm_compiler2_test_Compiler2Test.cpp
2 
3  Copyright (C) 1996-2014
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 "vm/vm.hpp"
29 #include "vm/string.hpp"
30 
31 #include "vm/jit/argument.hpp"
32 #include "vm/jit/jit.hpp"
33 
34 #include "threads/thread.hpp"
35 
36 #include "native/jni.hpp"
37 #include "native/llni.hpp"
38 #include "native/native.hpp"
39 
41 
42 // Native functions are exported as C functions.
43 extern "C" {
44 
45 /*
46  * Class: org/cacaojvm/compiler2/test/Compiler2Test
47  * Method: compileMethod
48  * Signature: (ZLjava/lang/Class;Ljava/lang/String;Ljava/lang/String;)V
49  */
50 JNIEXPORT void JNICALL Java_org_cacaojvm_compiler2_test_Compiler2Test_compileMethod(JNIEnv *env, jclass clazz, jboolean baseline, jclass compile_class, jstring name, jstring desc) {
51  classinfo *ci;
52 
53  ci = LLNI_classinfo_unwrap(compile_class);
54 
55  if (!ci) {
57  os::abort();
58  }
59 
60  if (name == NULL) {
62  os::abort();
63  }
64 
65  // create utf string in which '.' is replaced by '/'
68 
69  // find the method of the class
70  methodinfo *m = class_resolveclassmethod(ci, u, d, NULL, false);
71 
74  os::abort();
75  }
76 
77  // there is no main method or it isn't static
78  if ((m == NULL) || !(m->flags & ACC_STATIC)) {
82  os::abort();
83  }
84 
85  m->code = NULL;
86 
87  if (baseline) {
88  // baseline compiler
89  if (!jit_compile(m)) {
90  os::abort();
91  }
92  }
93  else {
94  // compiler2 compiler
96  os::abort();
97  }
98  }
99 
100  return;
101 }
102 
103 /*
104  * Class: org/cacaojvm/compiler2/test/Compiler2Test
105  * Method: executeMethod
106  * Signature: (ZLjava/lang/Class;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;
107  */
109  classinfo *ci;
110 
111  ci = LLNI_classinfo_unwrap(compile_class);
112 
113  if (!ci) {
115  os::abort();
116  }
117 
118  if (name == NULL) {
120  os::abort();
121  }
122 
123  // create utf string in which '.' is replaced by '/'
126 
127  // find the method of the class
128  methodinfo *m = class_resolveclassmethod(ci, u, d, NULL, false);
129 
130  if (exceptions_get_exception()) {
132  os::abort();
133  }
134 
135  // there is no main method or it isn't static
136  if ((m == NULL) || !(m->flags & ACC_STATIC)) {
140  os::abort();
141  }
142 
143  java_handle_t *result = vm_call_method_objectarray(m, NULL, args);
144 
145  // restore code
146  // TODO free code!
147  m->code = NULL;
148 
149  // exception occurred?
150  if (exceptions_get_exception()) {
152  os::abort();
153  }
154 
155  return (jobject) result;
156 }
157 
158 } // extern "C"
159 
160 /* native methods implemented by this file ************************************/
161 
162 static JNINativeMethod methods[] = {
163  { (char*) "compileMethod", (char*) "(ZLjava/lang/Class;Ljava/lang/String;Ljava/lang/String;)V",(void*) (uintptr_t) &Java_org_cacaojvm_compiler2_test_Compiler2Test_compileMethod },
164  { (char*) "executeMethod", (char*) "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;",(void*) (uintptr_t) &Java_org_cacaojvm_compiler2_test_Compiler2Test_executeMethod },
165 };
166 
167 
168 /* _Jv_org_cacaojvm_compiler2_test_Compiler2Test_init *****************************************
169 
170  Register native functions.
171 
172 *******************************************************************************/
173 
175  Utf8String u = Utf8String::from_utf8("org/cacaojvm/compiler2/test/Compiler2Test");
176 
179 }
180 
181 
182 /*
183  * These are local overrides for various environment variables in Emacs.
184  * Please do not remove this and leave it at the end of the file, where
185  * Emacs will automagically detect them.
186  * ---------------------------------------------------------------------
187  * Local variables:
188  * mode: c++
189  * indent-tabs-mode: t
190  * c-basic-offset: 4
191  * tab-width: 4
192  * End:
193  * vim:noexpandtab:sw=4:ts=4:
194  */
methodinfo * class_resolveclassmethod(classinfo *c, Utf8String name, Utf8String desc, classinfo *referer, bool throwexception)
Definition: class.cpp:1211
void exceptions_print_stacktrace(void)
Table containing all native methods registered with the VM.
Definition: native.hpp:132
NativeMethods & get_nativemethods()
Definition: vm.hpp:128
void register_methods(Utf8String classname, const JNINativeMethod *methods, size_t count)
Register native methods with the VM.
Definition: native.cpp:242
_Jv_JNIEnv JNIEnv
Definition: jni.hpp:112
void exceptions_throw_nosuchmethoderror(classinfo *c, Utf8String name, Utf8String desc)
Definition: exceptions.cpp:889
#define NATIVE_METHODS_COUNT
Definition: native.hpp:45
u1 * jit_compile(methodinfo *m)
Definition: jit.cpp:274
MachineCode * compile(methodinfo *m)
Definition: Compiler.cpp:123
JNIEnv jclass jobject const char * name
Definition: jvmti.h:312
Second stage compiler class.
JNIEXPORT jobject JNICALL Java_org_cacaojvm_compiler2_test_Compiler2Test_executeMethod(JNIEnv *env, jclass clazz, jclass compile_class, jstring name, jstring desc, jobjectArray args)
static JNINativeMethod methods[]
void exceptions_throw_nullpointerexception(void)
void _Jv_org_cacaojvm_compiler2_test_Compiler2Test_init(void)
JNIEXPORT void JNICALL Java_org_cacaojvm_compiler2_test_Compiler2Test_compileMethod(JNIEnv *env, jclass clazz, jboolean baseline, jclass compile_class, jstring name, jstring desc)
static Utf8String from_utf8(const char *, size_t)
Definition: utf8.cpp:335
static void abort()
Definition: os.hpp:196
codeinfo * code
Definition: method.hpp:103
java_handle_t * vm_call_method_objectarray(methodinfo *m, java_handle_t *o, java_handle_objectarray_t *params)
Definition: vm.cpp:2483
void exceptions_clear_exception(void)
Definition: exceptions.cpp:127
java_handle_t * exceptions_get_exception(void)
Definition: exceptions.cpp:76
#define LLNI_classinfo_unwrap(clazz)
Definition: llni.hpp:113
s4 flags
Definition: method.hpp:70
static VM * get_current()
Definition: vm.hpp:99
Utf8String to_utf8_dot_to_slash() const
Definition: string.cpp:450