1 /*
   2  * Copyright (c) 1997, 2014, 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 #ifdef TARGET_ARCH_aarch64
  50 # include "jniTypes_aarch64.hpp"
  51 #endif
  52 
  53 // A JavaCallWrapper is constructed before each JavaCall and destructed after the call.
  54 // Its purpose is to allocate/deallocate a new handle block and to save/restore the last
  55 // Java fp/sp. A pointer to the JavaCallWrapper is stored on the stack.
  56 
  57 class JavaCallWrapper: StackObj {
  58   friend class VMStructs;
  59  private:
  60   JavaThread*      _thread;                 // the thread to which this call belongs
  61   JNIHandleBlock*  _handles;                // the saved handle block
  62   Method*          _callee_method;          // to be able to collect arguments if entry frame is top frame
  63   oop              _receiver;               // the receiver of the call (if a non-static call)
  64 
  65   JavaFrameAnchor  _anchor;                 // last thread anchor state that we must restore
  66 
  67   JavaValue*       _result;                 // result value
  68 
  69  public:
  70   // Construction/destruction
  71    JavaCallWrapper(methodHandle callee_method, Handle receiver, JavaValue* result, TRAPS);
  72   ~JavaCallWrapper();
  73 
  74   // Accessors
  75   JavaThread*      thread() const           { return _thread; }
  76   JNIHandleBlock*  handles() const          { return _handles; }
  77 
  78   JavaFrameAnchor* anchor(void)             { return &_anchor; }
  79 
  80   JavaValue*       result() const           { return _result; }
  81   // GC support
  82   Method*          callee_method()          { return _callee_method; }
  83   oop              receiver()               { return _receiver; }
  84   void             oops_do(OopClosure* f);
  85 
  86   bool             is_first_frame() const   { return _anchor.last_Java_sp() == NULL; }
  87 
  88 };
  89 
  90 
  91 // Encapsulates arguments to a JavaCall (faster, safer, and more convenient than using var-args)
  92 class JavaCallArguments : public StackObj {
  93  private:
  94   enum Constants {
  95    _default_size = 8    // Must be at least # of arguments in JavaCalls methods
  96   };
  97 
  98   intptr_t    _value_buffer [_default_size + 1];
  99   bool        _is_oop_buffer[_default_size + 1];
 100 
 101   intptr_t*   _value;
 102   bool*       _is_oop;
 103   int         _size;
 104   int         _max_size;
 105   bool        _start_at_zero;      // Support late setting of receiver
 106   JVMCI_ONLY(nmethod*    _alternative_target;) // Nmethod that should be called instead of normal target
 107 
 108   void initialize() {
 109     // Starts at first element to support set_receiver.
 110     _value    = &_value_buffer[1];
 111     _is_oop   = &_is_oop_buffer[1];
 112 
 113     _max_size = _default_size;
 114     _size = 0;
 115     _start_at_zero = false;
 116     JVMCI_ONLY(_alternative_target = NULL;)
 117   }
 118 
 119  public:
 120   JavaCallArguments() { initialize(); }
 121 
 122   JavaCallArguments(Handle receiver) {
 123     initialize();
 124     push_oop(receiver);
 125   }
 126 
 127   JavaCallArguments(int max_size) {
 128     if (max_size > _default_size) {
 129       _value  = NEW_RESOURCE_ARRAY(intptr_t, max_size + 1);
 130       _is_oop = NEW_RESOURCE_ARRAY(bool, max_size + 1);
 131 
 132       // Reserve room for potential receiver in value and is_oop
 133       _value++; _is_oop++;
 134 
 135       _max_size = max_size;
 136       _size = 0;
 137       _start_at_zero = false;
 138       JVMCI_ONLY(_alternative_target = NULL;)
 139     } else {
 140       initialize();
 141     }
 142   }
 143 
 144 #if INCLUDE_JVMCI
 145   void set_alternative_target(nmethod* target) {
 146     _alternative_target = target;
 147   }
 148 
 149   nmethod* alternative_target() {
 150     return _alternative_target;
 151   }
 152 #endif
 153 
 154   inline void push_oop(Handle h)    { _is_oop[_size] = true;
 155                                JNITypes::put_obj((oop)h.raw_value(), _value, _size); }
 156 
 157   inline void push_int(int i)       { _is_oop[_size] = false;
 158                                JNITypes::put_int(i, _value, _size); }
 159 
 160   inline void push_double(double d) { _is_oop[_size] = false; _is_oop[_size + 1] = false;
 161                                JNITypes::put_double(d, _value, _size); }
 162 
 163   inline void push_long(jlong l)    { _is_oop[_size] = false; _is_oop[_size + 1] = false;
 164                                JNITypes::put_long(l, _value, _size); }
 165 
 166   inline void push_float(float f)   { _is_oop[_size] = false;
 167                                JNITypes::put_float(f, _value, _size); }
 168 
 169   // receiver
 170   Handle receiver() {
 171     assert(_size > 0, "must at least be one argument");
 172     assert(_is_oop[0], "first argument must be an oop");
 173     assert(_value[0] != 0, "receiver must be not-null");
 174     return Handle((oop*)_value[0], false);
 175   }
 176 
 177   void set_receiver(Handle h) {
 178     assert(_start_at_zero == false, "can only be called once");
 179     _start_at_zero = true;
 180     _is_oop--;
 181     _value--;
 182     _size++;
 183     _is_oop[0] = true;
 184     _value[0] = (intptr_t)h.raw_value();
 185   }
 186 
 187   // Converts all Handles to oops, and returns a reference to parameter vector
 188   intptr_t* parameters() ;
 189   int   size_of_parameters() const { return _size; }
 190 
 191   // Verify that pushed arguments fits a given method
 192   void verify(methodHandle method, BasicType return_type, Thread *thread);
 193 };
 194 
 195 // All calls to Java have to go via JavaCalls. Sets up the stack frame
 196 // and makes sure that the last_Java_frame pointers are chained correctly.
 197 //
 198 
 199 class JavaCalls: AllStatic {
 200   static void call_helper(JavaValue* result, methodHandle* method, JavaCallArguments* args, TRAPS);
 201  public:
 202   // call_special
 203   // ------------
 204   // The receiver must be first oop in argument list
 205   static void call_special(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS);
 206 
 207   static void call_special(JavaValue* result, Handle receiver, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS); // No args
 208   static void call_special(JavaValue* result, Handle receiver, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS);
 209   static void call_special(JavaValue* result, Handle receiver, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS);
 210 
 211   // virtual call
 212   // ------------
 213 
 214   // The receiver must be first oop in argument list
 215   static void call_virtual(JavaValue* result, KlassHandle spec_klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS);
 216 
 217   static void call_virtual(JavaValue* result, Handle receiver, KlassHandle spec_klass, Symbol* name, Symbol* signature, TRAPS); // No args
 218   static void call_virtual(JavaValue* result, Handle receiver, KlassHandle spec_klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS);
 219   static void call_virtual(JavaValue* result, Handle receiver, KlassHandle spec_klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS);
 220 
 221   // Static call
 222   // -----------
 223   static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS);
 224 
 225   static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS);
 226   static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS);
 227   static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS);
 228 
 229   // Low-level interface
 230   static void call(JavaValue* result, methodHandle method, JavaCallArguments* args, TRAPS);
 231 };
 232 
 233 #endif // SHARE_VM_RUNTIME_JAVACALLS_HPP