aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@1: /* aoqi@1: * This file has been modified by Loongson Technology in 2015. These aoqi@1: * modifications are Copyright (c) 2015 Loongson Technology, and are made aoqi@1: * available on the same license terms set forth above. aoqi@1: */ aoqi@1: aoqi@0: #ifndef SHARE_VM_RUNTIME_JAVACALLS_HPP aoqi@0: #define SHARE_VM_RUNTIME_JAVACALLS_HPP aoqi@0: aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "oops/method.hpp" aoqi@0: #include "runtime/handles.hpp" aoqi@0: #include "runtime/javaFrameAnchor.hpp" aoqi@0: #include "runtime/thread.inline.hpp" aoqi@0: #include "runtime/vmThread.hpp" aoqi@0: #ifdef TARGET_ARCH_x86 aoqi@0: # include "jniTypes_x86.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_sparc aoqi@0: # include "jniTypes_sparc.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_zero aoqi@0: # include "jniTypes_zero.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_arm aoqi@0: # include "jniTypes_arm.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_ppc aoqi@0: # include "jniTypes_ppc.hpp" aoqi@0: #endif aoqi@1: #ifdef TARGET_ARCH_mips aoqi@1: # include "jniTypes_mips.hpp" aoqi@1: #endif aoqi@0: aoqi@0: // A JavaCallWrapper is constructed before each JavaCall and destructed after the call. aoqi@0: // Its purpose is to allocate/deallocate a new handle block and to save/restore the last aoqi@0: // Java fp/sp. A pointer to the JavaCallWrapper is stored on the stack. aoqi@0: aoqi@0: class JavaCallWrapper: StackObj { aoqi@0: friend class VMStructs; aoqi@0: private: aoqi@0: JavaThread* _thread; // the thread to which this call belongs aoqi@0: JNIHandleBlock* _handles; // the saved handle block aoqi@0: Method* _callee_method; // to be able to collect arguments if entry frame is top frame aoqi@0: oop _receiver; // the receiver of the call (if a non-static call) aoqi@0: aoqi@0: JavaFrameAnchor _anchor; // last thread anchor state that we must restore aoqi@0: aoqi@0: JavaValue* _result; // result value aoqi@0: aoqi@0: public: aoqi@0: // Construction/destruction aoqi@0: JavaCallWrapper(methodHandle callee_method, Handle receiver, JavaValue* result, TRAPS); aoqi@0: ~JavaCallWrapper(); aoqi@0: aoqi@0: // Accessors aoqi@0: JavaThread* thread() const { return _thread; } aoqi@0: JNIHandleBlock* handles() const { return _handles; } aoqi@0: aoqi@0: JavaFrameAnchor* anchor(void) { return &_anchor; } aoqi@0: aoqi@0: JavaValue* result() const { return _result; } aoqi@0: // GC support aoqi@0: Method* callee_method() { return _callee_method; } aoqi@0: oop receiver() { return _receiver; } aoqi@0: void oops_do(OopClosure* f); aoqi@0: aoqi@0: bool is_first_frame() const { return _anchor.last_Java_sp() == NULL; } aoqi@0: aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // Encapsulates arguments to a JavaCall (faster, safer, and more convenient than using var-args) aoqi@0: class JavaCallArguments : public StackObj { aoqi@0: private: aoqi@0: enum Constants { aoqi@0: _default_size = 8 // Must be at least # of arguments in JavaCalls methods aoqi@0: }; aoqi@0: aoqi@0: intptr_t _value_buffer [_default_size + 1]; aoqi@0: bool _is_oop_buffer[_default_size + 1]; aoqi@0: aoqi@0: intptr_t* _value; aoqi@0: bool* _is_oop; aoqi@0: int _size; aoqi@0: int _max_size; aoqi@0: bool _start_at_zero; // Support late setting of receiver aoqi@0: aoqi@0: void initialize() { aoqi@0: // Starts at first element to support set_receiver. aoqi@0: _value = &_value_buffer[1]; aoqi@0: _is_oop = &_is_oop_buffer[1]; aoqi@0: aoqi@0: _max_size = _default_size; aoqi@0: _size = 0; aoqi@0: _start_at_zero = false; aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: JavaCallArguments() { initialize(); } aoqi@0: aoqi@0: JavaCallArguments(Handle receiver) { aoqi@0: initialize(); aoqi@0: push_oop(receiver); aoqi@0: } aoqi@0: aoqi@0: JavaCallArguments(int max_size) { aoqi@0: if (max_size > _default_size) { aoqi@0: _value = NEW_RESOURCE_ARRAY(intptr_t, max_size + 1); aoqi@0: _is_oop = NEW_RESOURCE_ARRAY(bool, max_size + 1); aoqi@0: aoqi@0: // Reserve room for potential receiver in value and is_oop aoqi@0: _value++; _is_oop++; aoqi@0: aoqi@0: _max_size = max_size; aoqi@0: _size = 0; aoqi@0: _start_at_zero = false; aoqi@0: } else { aoqi@0: initialize(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: inline void push_oop(Handle h) { _is_oop[_size] = true; aoqi@0: JNITypes::put_obj((oop)h.raw_value(), _value, _size); } aoqi@0: aoqi@0: inline void push_int(int i) { _is_oop[_size] = false; aoqi@0: JNITypes::put_int(i, _value, _size); } aoqi@0: aoqi@0: inline void push_double(double d) { _is_oop[_size] = false; _is_oop[_size + 1] = false; aoqi@0: JNITypes::put_double(d, _value, _size); } aoqi@0: aoqi@0: inline void push_long(jlong l) { _is_oop[_size] = false; _is_oop[_size + 1] = false; aoqi@0: JNITypes::put_long(l, _value, _size); } aoqi@0: aoqi@0: inline void push_float(float f) { _is_oop[_size] = false; aoqi@0: JNITypes::put_float(f, _value, _size); } aoqi@0: aoqi@0: // receiver aoqi@0: Handle receiver() { aoqi@0: assert(_size > 0, "must at least be one argument"); aoqi@0: assert(_is_oop[0], "first argument must be an oop"); aoqi@0: assert(_value[0] != 0, "receiver must be not-null"); aoqi@0: return Handle((oop*)_value[0], false); aoqi@0: } aoqi@0: aoqi@0: void set_receiver(Handle h) { aoqi@0: assert(_start_at_zero == false, "can only be called once"); aoqi@0: _start_at_zero = true; aoqi@0: _is_oop--; aoqi@0: _value--; aoqi@0: _size++; aoqi@0: _is_oop[0] = true; aoqi@0: _value[0] = (intptr_t)h.raw_value(); aoqi@0: } aoqi@0: aoqi@0: // Converts all Handles to oops, and returns a reference to parameter vector aoqi@0: intptr_t* parameters() ; aoqi@0: int size_of_parameters() const { return _size; } aoqi@0: aoqi@0: // Verify that pushed arguments fits a given method aoqi@0: void verify(methodHandle method, BasicType return_type, Thread *thread); aoqi@0: }; aoqi@0: aoqi@0: // All calls to Java have to go via JavaCalls. Sets up the stack frame aoqi@0: // and makes sure that the last_Java_frame pointers are chained correctly. aoqi@0: // aoqi@0: aoqi@0: class JavaCalls: AllStatic { aoqi@0: static void call_helper(JavaValue* result, methodHandle* method, JavaCallArguments* args, TRAPS); aoqi@0: public: aoqi@0: // Optimized Constuctor call aoqi@0: static void call_default_constructor(JavaThread* thread, methodHandle method, Handle receiver, TRAPS); aoqi@0: aoqi@0: // call_special aoqi@0: // ------------ aoqi@0: // The receiver must be first oop in argument list aoqi@0: static void call_special(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS); aoqi@0: aoqi@0: static void call_special(JavaValue* result, Handle receiver, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS); // No args aoqi@0: static void call_special(JavaValue* result, Handle receiver, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS); aoqi@0: static void call_special(JavaValue* result, Handle receiver, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS); aoqi@0: aoqi@0: // virtual call aoqi@0: // ------------ aoqi@0: aoqi@0: // The receiver must be first oop in argument list aoqi@0: static void call_virtual(JavaValue* result, KlassHandle spec_klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS); aoqi@0: aoqi@0: static void call_virtual(JavaValue* result, Handle receiver, KlassHandle spec_klass, Symbol* name, Symbol* signature, TRAPS); // No args aoqi@0: static void call_virtual(JavaValue* result, Handle receiver, KlassHandle spec_klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS); aoqi@0: static void call_virtual(JavaValue* result, Handle receiver, KlassHandle spec_klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS); aoqi@0: aoqi@0: // Static call aoqi@0: // ----------- aoqi@0: static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS); aoqi@0: aoqi@0: static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS); aoqi@0: static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS); aoqi@0: static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS); aoqi@0: aoqi@0: // Low-level interface aoqi@0: static void call(JavaValue* result, methodHandle method, JavaCallArguments* args, TRAPS); aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_RUNTIME_JAVACALLS_HPP