javaCalls.hpp 9.5 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28
#ifndef SHARE_VM_RUNTIME_JAVACALLS_HPP
#define SHARE_VM_RUNTIME_JAVACALLS_HPP

#include "memory/allocation.hpp"
29
#include "oops/method.hpp"
30 31
#include "runtime/handles.hpp"
#include "runtime/javaFrameAnchor.hpp"
32
#include "runtime/thread.inline.hpp"
33 34 35 36 37 38 39 40 41 42
#include "runtime/vmThread.hpp"
#ifdef TARGET_ARCH_x86
# include "jniTypes_x86.hpp"
#endif
#ifdef TARGET_ARCH_sparc
# include "jniTypes_sparc.hpp"
#endif
#ifdef TARGET_ARCH_zero
# include "jniTypes_zero.hpp"
#endif
43 44 45 46 47 48
#ifdef TARGET_ARCH_arm
# include "jniTypes_arm.hpp"
#endif
#ifdef TARGET_ARCH_ppc
# include "jniTypes_ppc.hpp"
#endif
49

D
duke 已提交
50 51 52 53 54 55 56 57 58
// A JavaCallWrapper is constructed before each JavaCall and destructed after the call.
// Its purpose is to allocate/deallocate a new handle block and to save/restore the last
// Java fp/sp. A pointer to the JavaCallWrapper is stored on the stack.

class JavaCallWrapper: StackObj {
  friend class VMStructs;
 private:
  JavaThread*      _thread;                 // the thread to which this call belongs
  JNIHandleBlock*  _handles;                // the saved handle block
59
  Method*          _callee_method;          // to be able to collect arguments if entry frame is top frame
D
duke 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  oop              _receiver;               // the receiver of the call (if a non-static call)

  JavaFrameAnchor  _anchor;                 // last thread anchor state that we must restore

  JavaValue*       _result;                 // result value

 public:
  // Construction/destruction
   JavaCallWrapper(methodHandle callee_method, Handle receiver, JavaValue* result, TRAPS);
  ~JavaCallWrapper();

  // Accessors
  JavaThread*      thread() const           { return _thread; }
  JNIHandleBlock*  handles() const          { return _handles; }

  JavaFrameAnchor* anchor(void)             { return &_anchor; }

  JavaValue*       result() const           { return _result; }
  // GC support
79
  Method*          callee_method()          { return _callee_method; }
D
duke 已提交
80 81 82
  oop              receiver()               { return _receiver; }
  void             oops_do(OopClosure* f);

83 84
  bool             is_first_frame() const   { return _anchor.last_Java_sp() == NULL; }

D
duke 已提交
85 86 87 88 89 90 91 92 93 94
};


// Encapsulates arguments to a JavaCall (faster, safer, and more convenient than using var-args)
class JavaCallArguments : public StackObj {
 private:
  enum Constants {
   _default_size = 8    // Must be at least # of arguments in JavaCalls methods
  };

95 96
  intptr_t    _value_buffer      [_default_size + 1];
  u_char      _value_state_buffer[_default_size + 1];
D
duke 已提交
97 98

  intptr_t*   _value;
99
  u_char*     _value_state;
D
duke 已提交
100 101 102 103 104 105
  int         _size;
  int         _max_size;
  bool        _start_at_zero;      // Support late setting of receiver

  void initialize() {
    // Starts at first element to support set_receiver.
106 107
    _value       = &_value_buffer[1];
    _value_state = &_value_state_buffer[1];
D
duke 已提交
108 109 110 111 112 113

    _max_size = _default_size;
    _size = 0;
    _start_at_zero = false;
  }

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  // Helper for push_oop and the like.  The value argument is a
  // "handle" that refers to an oop.  We record the address of the
  // handle rather than the designated oop.  The handle is later
  // resolved to the oop by parameters().  This delays the exposure of
  // naked oops until it is GC-safe.
  template<typename T>
  inline int push_oop_impl(T handle, int size) {
    // JNITypes::put_obj expects an oop value, so we play fast and
    // loose with the type system.  The cast from handle type to oop
    // *must* use a C-style cast.  In a product build it performs a
    // reinterpret_cast. In a debug build (more accurately, in a
    // CHECK_UNHANDLED_OOPS build) it performs a static_cast, invoking
    // the debug-only oop class's conversion from void* constructor.
    JNITypes::put_obj((oop)handle, _value, size); // Updates size.
    return size;                // Return the updated size.
  }

D
duke 已提交
131 132 133 134 135 136 137 138 139 140
 public:
  JavaCallArguments() { initialize(); }

  JavaCallArguments(Handle receiver) {
    initialize();
    push_oop(receiver);
  }

  JavaCallArguments(int max_size) {
    if (max_size > _default_size) {
141 142
      _value = NEW_RESOURCE_ARRAY(intptr_t, max_size + 1);
      _value_state = NEW_RESOURCE_ARRAY(u_char, max_size + 1);
143

144 145 146
      // Reserve room for potential receiver in value and state
      _value++;
      _value_state++;
147

D
duke 已提交
148 149 150 151 152 153 154 155
      _max_size = max_size;
      _size = 0;
      _start_at_zero = false;
    } else {
      initialize();
    }
  }

156 157 158 159 160 161 162 163
  // The possible values for _value_state elements.
  enum {
    value_state_primitive,
    value_state_oop,
    value_state_handle,
    value_state_jobject,
    value_state_limit
  };
D
duke 已提交
164

165 166 167 168
  inline void push_oop(Handle h) {
    _value_state[_size] = value_state_handle;
    _size = push_oop_impl(h.raw_value(), _size);
  }
D
duke 已提交
169

170 171 172 173
  inline void push_jobject(jobject h) {
    _value_state[_size] = value_state_jobject;
    _size = push_oop_impl(h, _size);
  }
D
duke 已提交
174

175 176 177 178
  inline void push_int(int i) {
    _value_state[_size] = value_state_primitive;
    JNITypes::put_int(i, _value, _size);
  }
D
duke 已提交
179

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
  inline void push_double(double d) {
    _value_state[_size] = value_state_primitive;
    _value_state[_size + 1] = value_state_primitive;
    JNITypes::put_double(d, _value, _size);
  }

  inline void push_long(jlong l) {
    _value_state[_size] = value_state_primitive;
    _value_state[_size + 1] = value_state_primitive;
    JNITypes::put_long(l, _value, _size);
  }

  inline void push_float(float f) {
    _value_state[_size] = value_state_primitive;
    JNITypes::put_float(f, _value, _size);
  }
D
duke 已提交
196 197 198 199

  // receiver
  Handle receiver() {
    assert(_size > 0, "must at least be one argument");
200 201
    assert(_value_state[0] == value_state_handle,
           "first argument must be an oop");
D
duke 已提交
202 203 204 205 206 207 208
    assert(_value[0] != 0, "receiver must be not-null");
    return Handle((oop*)_value[0], false);
  }

  void set_receiver(Handle h) {
    assert(_start_at_zero == false, "can only be called once");
    _start_at_zero = true;
209
    _value_state--;
D
duke 已提交
210 211
    _value--;
    _size++;
212 213
    _value_state[0] = value_state_handle;
    push_oop_impl(h.raw_value(), 0);
D
duke 已提交
214 215 216 217 218 219 220
  }

  // Converts all Handles to oops, and returns a reference to parameter vector
  intptr_t* parameters() ;
  int   size_of_parameters() const { return _size; }

  // Verify that pushed arguments fits a given method
221
  void verify(methodHandle method, BasicType return_type);
D
duke 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
};

// All calls to Java have to go via JavaCalls. Sets up the stack frame
// and makes sure that the last_Java_frame pointers are chained correctly.
//

class JavaCalls: AllStatic {
  static void call_helper(JavaValue* result, methodHandle* method, JavaCallArguments* args, TRAPS);
 public:
  // Optimized Constuctor call
  static void call_default_constructor(JavaThread* thread, methodHandle method, Handle receiver, TRAPS);

  // call_special
  // ------------
  // The receiver must be first oop in argument list
237
  static void call_special(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS);
D
duke 已提交
238

239 240 241
  static void call_special(JavaValue* result, Handle receiver, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS); // No args
  static void call_special(JavaValue* result, Handle receiver, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS);
  static void call_special(JavaValue* result, Handle receiver, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS);
D
duke 已提交
242 243 244 245 246

  // virtual call
  // ------------

  // The receiver must be first oop in argument list
247
  static void call_virtual(JavaValue* result, KlassHandle spec_klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS);
D
duke 已提交
248

249 250 251
  static void call_virtual(JavaValue* result, Handle receiver, KlassHandle spec_klass, Symbol* name, Symbol* signature, TRAPS); // No args
  static void call_virtual(JavaValue* result, Handle receiver, KlassHandle spec_klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS);
  static void call_virtual(JavaValue* result, Handle receiver, KlassHandle spec_klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS);
D
duke 已提交
252 253 254

  // Static call
  // -----------
255
  static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, JavaCallArguments* args, TRAPS);
D
duke 已提交
256

257 258 259
  static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS);
  static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, TRAPS);
  static void call_static(JavaValue* result, KlassHandle klass, Symbol* name, Symbol* signature, Handle arg1, Handle arg2, TRAPS);
D
duke 已提交
260 261 262 263

  // Low-level interface
  static void call(JavaValue* result, methodHandle method, JavaCallArguments* args, TRAPS);
};
264 265

#endif // SHARE_VM_RUNTIME_JAVACALLS_HPP