1 /* 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_RUNTIME_JAVACALLS_HPP 26 #define SHARE_VM_RUNTIME_JAVACALLS_HPP 27 28 #include "memory/allocation.hpp" 29 #include "oops/method.hpp" 30 #include "runtime/handles.hpp" 31 #include "runtime/javaFrameAnchor.hpp" 32 #include "runtime/thread.inline.hpp" 33 #include "runtime/vmThread.hpp" 34 #ifdef TARGET_ARCH_x86 35 # include "jniTypes_x86.hpp" 36 #endif 37 #ifdef TARGET_ARCH_sparc 38 # include "jniTypes_sparc.hpp" 39 #endif 40 #ifdef TARGET_ARCH_zero 41 # include "jniTypes_zero.hpp" 42 #endif 43 #ifdef TARGET_ARCH_arm 44 # include "jniTypes_arm.hpp" 45 #endif 46 #ifdef TARGET_ARCH_ppc 47 # include "jniTypes_ppc.hpp" 48 #endif 49 50 // A JavaCallWrapper is constructed before each JavaCall and destructed after the call. 51 // Its purpose is to allocate/deallocate a new handle block and to save/restore the last 52 // Java fp/sp. A pointer to the JavaCallWrapper is stored on the stack. 53 54 class JavaCallWrapper: StackObj { 55 friend class VMStructs; 56 private: 57 JavaThread* _thread; // the thread to which this call belongs 58 JNIHandleBlock* _handles; // the saved handle block 59 Method* _callee_method; // to be able to collect arguments if entry frame is top frame 60 oop _receiver; // the receiver of the call (if a non-static call) 61 62 JavaFrameAnchor _anchor; // last thread anchor state that we must restore 63 64 JavaValue* _result; // result value 65 66 public: 67 // Construction/destruction 68 JavaCallWrapper(methodHandle callee_method, Handle receiver, JavaValue* result, TRAPS); 69 ~JavaCallWrapper(); 70 71 // Accessors 72 JavaThread* thread() const { return _thread; } 73 JNIHandleBlock* handles() const { return _handles; } 74 75 JavaFrameAnchor* anchor(void) { return &_anchor; } 76 77 JavaValue* result() const { return _result; } 78 // GC support 79 Method* callee_method() { return _callee_method; } 80 oop receiver() { return _receiver; } 81 void oops_do(OopClosure* f); 82 83 }; 84 85 86 // Encapsulates arguments to a JavaCall (faster, safer, and more convenient than using var-args) 87 class JavaCallArguments : public StackObj { 88 private: 89 enum Constants { 90 _default_size = 8 // Must be at least # of arguments in JavaCalls methods 91 }; 92 93 intptr_t _value_buffer [_default_size + 1]; 94 bool _is_oop_buffer[_default_size + 1]; 95 96 intptr_t* _value; 97 bool* _is_oop; 98 int _size; 99 int _max_size; 100 bool _start_at_zero; // Support late setting of receiver 101 102 void initialize() { 103 // Starts at first element to support set_receiver. 104 _value = &_value_buffer[1]; 105 _is_oop = &_is_oop_buffer[1]; 106 107 _max_size = _default_size; 108 _size = 0; 109 _start_at_zero = false; 110 } 111 112 public: 113 JavaCallArguments() { initialize(); } 114 115 JavaCallArguments(Handle receiver) { 116 initialize(); 117 push_oop(receiver); 118 } 119 120 JavaCallArguments(int max_size) { 121 if (max_size > _default_size) { 122 _value = NEW_RESOURCE_ARRAY(intptr_t, max_size + 1); 123 _is_oop = NEW_RESOURCE_ARRAY(bool, max_size + 1); 124 125 // Reserve room for potential receiver in value and is_oop 126 _value++; _is_oop++; 127 128 _max_size = max_size; 129 _size = 0; 130 _start_at_zero = false; 131 } else { 132 initialize(); 133 } 134 } 135 136 inline void push_oop(Handle h) { _is_oop[_size] = true; 137 JNITypes::put_obj((oop)h.raw_value(), _value, _size); } 138 139 inline void push_int(int i) { _is_oop[_size] = false; 140 JNITypes::put_int(i, _value, _size); } 141 142 inline void push_double(double d) { _is_oop[_size] = false; _is_oop[_size + 1] = false; 143 JNITypes::put_double(d, _value, _size); } 144 145 inline void push_long(jlong l) { _is_oop[_size] = false; _is_oop[_size + 1] = false; 146 JNITypes::put_long(l, _value, _size); } 147 148 inline void push_float(float f) { _is_oop[_size] = false; 149 JNITypes::put_float(f, _value, _size); } 150 151 // receiver 152 Handle receiver() { 153 assert(_size > 0, "must at least be one argument"); 154 assert(_is_oop[0], "first argument must be an oop"); 155 assert(_value[0] != 0, "receiver must be not-null"); 156 return Handle((oop*)_value[0], false); 157 } 158 159 void set_receiver(Handle h) { 160 assert(_start_at_zero == false, "can only be called once"); 161 _start_at_zero = true; 162 _is_oop--; 163 _value--; 164 _size++; 165 _is_oop[0] = true; 166 _value[0] = (intptr_t)h.raw_value(); 167 } 168 169 // Converts all Handles to oops, and returns a reference to parameter vector 170 intptr_t* parameters() ; 171 int size_of_parameters() const { return _size; } 172 173 // Verify that pushed arguments fits a given method 174 void verify(methodHandle method, BasicType return_type, Thread *thread); 175 }; 176 177 // All calls to Java have to go via JavaCalls. Sets up the stack frame 178 // and makes sure that the last_Java_frame pointers are chained correctly. 179 // 180 181 class JavaCalls: AllStatic { 182 static void call_helper(JavaValue* result, methodHandle* method, JavaCallArguments* args, TRAPS); 183 public: 184 // Optimized Constuctor call 185 static void call_default_constructor(JavaThread* thread, methodHandle method, Handle receiver, TRAPS); 186 187 // call_special 188 // ------------ 189 // The receiver must be first oop in argument list 190 static void call_special(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS); 191 192 static void call_special(JavaValue* result, Handle receiver, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS); // No args 193 static void call_special(JavaValue* result, Handle receiver, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS); 194 static void call_special(JavaValue* result, Handle receiver, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS); 195 196 // virtual call 197 // ------------ 198 199 // The receiver must be first oop in argument list 200 static void call_virtual(JavaValue* result, KlassHandle spec_klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS); 201 202 static void call_virtual(JavaValue* result, Handle receiver, KlassHandle spec_klass, Symbol* name, Symbol* signature, TRAPS); // No args 203 static void call_virtual(JavaValue* result, Handle receiver, KlassHandle spec_klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS); 204 static void call_virtual(JavaValue* result, Handle receiver, KlassHandle spec_klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS); 205 206 // Static call 207 // ----------- 208 static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS); 209 210 static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS); 211 static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS); 212 static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS); 213 214 // Low-level interface 215 static void call(JavaValue* result, methodHandle method, JavaCallArguments* args, TRAPS); 216 }; 217 218 #endif // SHARE_VM_RUNTIME_JAVACALLS_HPP