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