hotspot/src/cpu/x86/vm/methodHandles_x86.hpp
changeset 13391 30245956af37
parent 11571 23f825a42a85
child 13728 882756847a04
equal deleted inserted replaced
13309:50c604cb0d5f 13391:30245956af37
    25 // Platform-specific definitions for method handles.
    25 // Platform-specific definitions for method handles.
    26 // These definitions are inlined into class MethodHandles.
    26 // These definitions are inlined into class MethodHandles.
    27 
    27 
    28 // Adapters
    28 // Adapters
    29 enum /* platform_dependent_constants */ {
    29 enum /* platform_dependent_constants */ {
    30   adapter_code_size = NOT_LP64(16000 DEBUG_ONLY(+ 15000)) LP64_ONLY(32000 DEBUG_ONLY(+ 120000))
    30   adapter_code_size = NOT_LP64(16000 DEBUG_ONLY(+ 25000)) LP64_ONLY(32000 DEBUG_ONLY(+ 150000))
    31 };
       
    32 
       
    33 public:
       
    34 
       
    35 // The stack just after the recursive call from a ricochet frame
       
    36 // looks something like this.  Offsets are marked in words, not bytes.
       
    37 // rsi (r13 on LP64) is part of the interpreter calling sequence
       
    38 // which tells the callee where is my real rsp (for frame walking).
       
    39 // (...lower memory addresses)
       
    40 // rsp:     [ return pc                 ]   always the global RicochetBlob::bounce_addr
       
    41 // rsp+1:   [ recursive arg N           ]
       
    42 // rsp+2:   [ recursive arg N-1         ]
       
    43 // ...
       
    44 // rsp+N:   [ recursive arg 1           ]
       
    45 // rsp+N+1: [ recursive method handle   ]
       
    46 // ...
       
    47 // rbp-6:   [ cleanup continuation pc   ]   <-- (struct RicochetFrame)
       
    48 // rbp-5:   [ saved target MH           ]   the MH we will call on the saved args
       
    49 // rbp-4:   [ saved args layout oop     ]   an int[] array which describes argument layout
       
    50 // rbp-3:   [ saved args pointer        ]   address of transformed adapter arg M (slot 0)
       
    51 // rbp-2:   [ conversion                ]   information about how the return value is used
       
    52 // rbp-1:   [ exact sender sp           ]   exact TOS (rsi/r13) of original sender frame
       
    53 // rbp+0:   [ saved sender fp           ]   (for original sender of AMH)
       
    54 // rbp+1:   [ saved sender pc           ]   (back to original sender of AMH)
       
    55 // rbp+2:   [ transformed adapter arg M ]   <-- (extended TOS of original sender)
       
    56 // rbp+3:   [ transformed adapter arg M-1]
       
    57 // ...
       
    58 // rbp+M+1: [ transformed adapter arg 1 ]
       
    59 // rbp+M+2: [ padding                   ] <-- (rbp + saved args base offset)
       
    60 // ...      [ optional padding]
       
    61 // (higher memory addresses...)
       
    62 //
       
    63 // The arguments originally passed by the original sender
       
    64 // are lost, and arbitrary amounts of stack motion might have
       
    65 // happened due to argument transformation.
       
    66 // (This is done by C2I/I2C adapters and non-direct method handles.)
       
    67 // This is why there is an unpredictable amount of memory between
       
    68 // the extended and exact TOS of the sender.
       
    69 // The ricochet adapter itself will also (in general) perform
       
    70 // transformations before the recursive call.
       
    71 //
       
    72 // The transformed and saved arguments, immediately above the saved
       
    73 // return PC, are a well-formed method handle invocation ready to execute.
       
    74 // When the GC needs to walk the stack, these arguments are described
       
    75 // via the saved arg types oop, an int[] array with a private format.
       
    76 // This array is derived from the type of the transformed adapter
       
    77 // method handle, which also sits at the base of the saved argument
       
    78 // bundle.  Since the GC may not be able to fish out the int[]
       
    79 // array, so it is pushed explicitly on the stack.  This may be
       
    80 // an unnecessary expense.
       
    81 //
       
    82 // The following register conventions are significant at this point:
       
    83 // rsp       the thread stack, as always; preserved by caller
       
    84 // rsi/r13   exact TOS of recursive frame (contents of [rbp-2])
       
    85 // rcx       recursive method handle (contents of [rsp+N+1])
       
    86 // rbp       preserved by caller (not used by caller)
       
    87 // Unless otherwise specified, all registers can be blown by the call.
       
    88 //
       
    89 // If this frame must be walked, the transformed adapter arguments
       
    90 // will be found with the help of the saved arguments descriptor.
       
    91 //
       
    92 // Therefore, the descriptor must match the referenced arguments.
       
    93 // The arguments must be followed by at least one word of padding,
       
    94 // which will be necessary to complete the final method handle call.
       
    95 // That word is not treated as holding an oop.  Neither is the word
       
    96 //
       
    97 // The word pointed to by the return argument pointer is not
       
    98 // treated as an oop, even if points to a saved argument.
       
    99 // This allows the saved argument list to have a "hole" in it
       
   100 // to receive an oop from the recursive call.
       
   101 // (The hole might temporarily contain RETURN_VALUE_PLACEHOLDER.)
       
   102 //
       
   103 // When the recursive callee returns, RicochetBlob::bounce_addr will
       
   104 // immediately jump to the continuation stored in the RF.
       
   105 // This continuation will merge the recursive return value
       
   106 // into the saved argument list.  At that point, the original
       
   107 // rsi, rbp, and rsp will be reloaded, the ricochet frame will
       
   108 // disappear, and the final target of the adapter method handle
       
   109 // will be invoked on the transformed argument list.
       
   110 
       
   111 class RicochetFrame {
       
   112   friend class MethodHandles;
       
   113   friend class VMStructs;
       
   114 
       
   115  private:
       
   116   intptr_t* _continuation;          // what to do when control gets back here
       
   117   oopDesc*  _saved_target;          // target method handle to invoke on saved_args
       
   118   oopDesc*  _saved_args_layout;     // caching point for MethodTypeForm.vmlayout cookie
       
   119   intptr_t* _saved_args_base;       // base of pushed arguments (slot 0, arg N) (-3)
       
   120   intptr_t  _conversion;            // misc. information from original AdapterMethodHandle (-2)
       
   121   intptr_t* _exact_sender_sp;       // parallel to interpreter_frame_sender_sp (-1)
       
   122   intptr_t* _sender_link;           // *must* coincide with frame::link_offset (0)
       
   123   address   _sender_pc;             // *must* coincide with frame::return_addr_offset (1)
       
   124 
       
   125  public:
       
   126   intptr_t* continuation() const        { return _continuation; }
       
   127   oop       saved_target() const        { return _saved_target; }
       
   128   oop       saved_args_layout() const   { return _saved_args_layout; }
       
   129   intptr_t* saved_args_base() const     { return _saved_args_base; }
       
   130   intptr_t  conversion() const          { return _conversion; }
       
   131   intptr_t* exact_sender_sp() const     { return _exact_sender_sp; }
       
   132   intptr_t* sender_link() const         { return _sender_link; }
       
   133   address   sender_pc() const           { return _sender_pc; }
       
   134 
       
   135   intptr_t* extended_sender_sp() const {
       
   136     // The extended sender SP is above the current RicochetFrame.
       
   137     return (intptr_t*) (((address) this) + sizeof(RicochetFrame));
       
   138   }
       
   139 
       
   140   intptr_t  return_value_slot_number() const {
       
   141     return adapter_conversion_vminfo(conversion());
       
   142   }
       
   143   BasicType return_value_type() const {
       
   144     return adapter_conversion_dest_type(conversion());
       
   145   }
       
   146   bool has_return_value_slot() const {
       
   147     return return_value_type() != T_VOID;
       
   148   }
       
   149   intptr_t* return_value_slot_addr() const {
       
   150     assert(has_return_value_slot(), "");
       
   151     return saved_arg_slot_addr(return_value_slot_number());
       
   152   }
       
   153   intptr_t* saved_target_slot_addr() const {
       
   154     return saved_arg_slot_addr(saved_args_length());
       
   155   }
       
   156   intptr_t* saved_arg_slot_addr(int slot) const {
       
   157     assert(slot >= 0, "");
       
   158     return (intptr_t*)( (address)saved_args_base() + (slot * Interpreter::stackElementSize) );
       
   159   }
       
   160 
       
   161   jint      saved_args_length() const;
       
   162   jint      saved_arg_offset(int arg) const;
       
   163 
       
   164   // GC interface
       
   165   oop*  saved_target_addr()                     { return (oop*)&_saved_target; }
       
   166   oop*  saved_args_layout_addr()                { return (oop*)&_saved_args_layout; }
       
   167 
       
   168   oop  compute_saved_args_layout(bool read_cache, bool write_cache);
       
   169 
       
   170   // Compiler/assembler interface.
       
   171   static int continuation_offset_in_bytes()     { return offset_of(RicochetFrame, _continuation); }
       
   172   static int saved_target_offset_in_bytes()     { return offset_of(RicochetFrame, _saved_target); }
       
   173   static int saved_args_layout_offset_in_bytes(){ return offset_of(RicochetFrame, _saved_args_layout); }
       
   174   static int saved_args_base_offset_in_bytes()  { return offset_of(RicochetFrame, _saved_args_base); }
       
   175   static int conversion_offset_in_bytes()       { return offset_of(RicochetFrame, _conversion); }
       
   176   static int exact_sender_sp_offset_in_bytes()  { return offset_of(RicochetFrame, _exact_sender_sp); }
       
   177   static int sender_link_offset_in_bytes()      { return offset_of(RicochetFrame, _sender_link); }
       
   178   static int sender_pc_offset_in_bytes()        { return offset_of(RicochetFrame, _sender_pc); }
       
   179 
       
   180   // This value is not used for much, but it apparently must be nonzero.
       
   181   static int frame_size_in_bytes()              { return sender_link_offset_in_bytes(); }
       
   182 
       
   183 #ifdef ASSERT
       
   184   // The magic number is supposed to help find ricochet frames within the bytes of stack dumps.
       
   185   enum { MAGIC_NUMBER_1 = 0xFEED03E, MAGIC_NUMBER_2 = 0xBEEF03E };
       
   186   static int magic_number_1_offset_in_bytes()   { return -wordSize; }
       
   187   static int magic_number_2_offset_in_bytes()   { return sizeof(RicochetFrame); }
       
   188   intptr_t magic_number_1() const               { return *(intptr_t*)((address)this + magic_number_1_offset_in_bytes()); };
       
   189   intptr_t magic_number_2() const               { return *(intptr_t*)((address)this + magic_number_2_offset_in_bytes()); };
       
   190 #endif //ASSERT
       
   191 
       
   192   enum { RETURN_VALUE_PLACEHOLDER = (NOT_DEBUG(0) DEBUG_ONLY(42)) };
       
   193 
       
   194   static void verify_offsets() NOT_DEBUG_RETURN;
       
   195   void verify() const NOT_DEBUG_RETURN; // check for MAGIC_NUMBER, etc.
       
   196   void zap_arguments() NOT_DEBUG_RETURN;
       
   197 
       
   198   static void generate_ricochet_blob(MacroAssembler* _masm,
       
   199                                      // output params:
       
   200                                      int* bounce_offset,
       
   201                                      int* exception_offset,
       
   202                                      int* frame_size_in_words);
       
   203 
       
   204   static void enter_ricochet_frame(MacroAssembler* _masm,
       
   205                                    Register rcx_recv,
       
   206                                    Register rax_argv,
       
   207                                    address return_handler,
       
   208                                    Register rbx_temp);
       
   209   static void leave_ricochet_frame(MacroAssembler* _masm,
       
   210                                    Register rcx_recv,
       
   211                                    Register new_sp_reg,
       
   212                                    Register sender_pc_reg);
       
   213 
       
   214   static Address frame_address(int offset = 0) {
       
   215     // The RicochetFrame is found by subtracting a constant offset from rbp.
       
   216     return Address(rbp, - sender_link_offset_in_bytes() + offset);
       
   217   }
       
   218 
       
   219   static RicochetFrame* from_frame(const frame& fr) {
       
   220     address bp = (address) fr.fp();
       
   221     RicochetFrame* rf = (RicochetFrame*)(bp - sender_link_offset_in_bytes());
       
   222     rf->verify();
       
   223     return rf;
       
   224   }
       
   225 
       
   226   static void verify_clean(MacroAssembler* _masm) NOT_DEBUG_RETURN;
       
   227 
       
   228   static void describe(const frame* fr, FrameValues& values, int frame_no) PRODUCT_RETURN;
       
   229 };
    31 };
   230 
    32 
   231 // Additional helper methods for MethodHandles code generation:
    33 // Additional helper methods for MethodHandles code generation:
   232 public:
    34 public:
   233   static void load_klass_from_Class(MacroAssembler* _masm, Register klass_reg);
    35   static void load_klass_from_Class(MacroAssembler* _masm, Register klass_reg);
   234   static void load_conversion_vminfo(MacroAssembler* _masm, Register reg, Address conversion_field_addr);
       
   235   static void load_conversion_dest_type(MacroAssembler* _masm, Register reg, Address conversion_field_addr);
       
   236 
       
   237   static void load_stack_move(MacroAssembler* _masm,
       
   238                               Register rdi_stack_move,
       
   239                               Register rcx_amh,
       
   240                               bool might_be_negative);
       
   241 
       
   242   static void insert_arg_slots(MacroAssembler* _masm,
       
   243                                RegisterOrConstant arg_slots,
       
   244                                Register rax_argslot,
       
   245                                Register rbx_temp, Register rdx_temp);
       
   246 
       
   247   static void remove_arg_slots(MacroAssembler* _masm,
       
   248                                RegisterOrConstant arg_slots,
       
   249                                Register rax_argslot,
       
   250                                Register rbx_temp, Register rdx_temp);
       
   251 
       
   252   static void push_arg_slots(MacroAssembler* _masm,
       
   253                                    Register rax_argslot,
       
   254                                    RegisterOrConstant slot_count,
       
   255                                    int skip_words_count,
       
   256                                    Register rbx_temp, Register rdx_temp);
       
   257 
       
   258   static void move_arg_slots_up(MacroAssembler* _masm,
       
   259                                 Register rbx_bottom,  // invariant
       
   260                                 Address  top_addr,    // can use rax_temp
       
   261                                 RegisterOrConstant positive_distance_in_slots,
       
   262                                 Register rax_temp, Register rdx_temp);
       
   263 
       
   264   static void move_arg_slots_down(MacroAssembler* _masm,
       
   265                                   Address  bottom_addr,  // can use rax_temp
       
   266                                   Register rbx_top,      // invariant
       
   267                                   RegisterOrConstant negative_distance_in_slots,
       
   268                                   Register rax_temp, Register rdx_temp);
       
   269 
       
   270   static void move_typed_arg(MacroAssembler* _masm,
       
   271                              BasicType type, bool is_element,
       
   272                              Address slot_dest, Address value_src,
       
   273                              Register rbx_temp, Register rdx_temp);
       
   274 
       
   275   static void move_return_value(MacroAssembler* _masm, BasicType type,
       
   276                                 Address return_slot);
       
   277 
       
   278   static void verify_argslot(MacroAssembler* _masm, Register argslot_reg,
       
   279                              const char* error_message) NOT_DEBUG_RETURN;
       
   280 
       
   281   static void verify_argslots(MacroAssembler* _masm,
       
   282                               RegisterOrConstant argslot_count,
       
   283                               Register argslot_reg,
       
   284                               bool negate_argslot,
       
   285                               const char* error_message) NOT_DEBUG_RETURN;
       
   286 
       
   287   static void verify_stack_move(MacroAssembler* _masm,
       
   288                                 RegisterOrConstant arg_slots,
       
   289                                 int direction) NOT_DEBUG_RETURN;
       
   290 
    36 
   291   static void verify_klass(MacroAssembler* _masm,
    37   static void verify_klass(MacroAssembler* _masm,
   292                            Register obj, KlassHandle klass,
    38                            Register obj, KlassHandle klass,
   293                            const char* error_message = "wrong klass") NOT_DEBUG_RETURN;
    39                            const char* error_message = "wrong klass") NOT_DEBUG_RETURN;
   294 
    40 
   295   static void verify_method_handle(MacroAssembler* _masm, Register mh_reg) {
    41   static void verify_method_handle(MacroAssembler* _masm, Register mh_reg) {
   296     verify_klass(_masm, mh_reg, SystemDictionaryHandles::MethodHandle_klass(),
    42     verify_klass(_masm, mh_reg, SystemDictionaryHandles::MethodHandle_klass(),
   297                  "reference is a MH");
    43                  "reference is a MH");
   298   }
    44   }
   299 
    45 
       
    46   static void verify_ref_kind(MacroAssembler* _masm, int ref_kind, Register member_reg, Register temp) NOT_DEBUG_RETURN;
       
    47 
   300   // Similar to InterpreterMacroAssembler::jump_from_interpreted.
    48   // Similar to InterpreterMacroAssembler::jump_from_interpreted.
   301   // Takes care of special dispatch from single stepping too.
    49   // Takes care of special dispatch from single stepping too.
   302   static void jump_from_method_handle(MacroAssembler* _masm, Register method, Register temp);
    50   static void jump_from_method_handle(MacroAssembler* _masm, Register method, Register temp,
       
    51                                       bool for_compiler_entry);
       
    52 
       
    53   static void jump_to_lambda_form(MacroAssembler* _masm,
       
    54                                   Register recv, Register method_temp,
       
    55                                   Register temp2,
       
    56                                   bool for_compiler_entry);
   303 
    57 
   304   static void trace_method_handle(MacroAssembler* _masm, const char* adaptername) PRODUCT_RETURN;
    58   static void trace_method_handle(MacroAssembler* _masm, const char* adaptername) PRODUCT_RETURN;
   305 
    59 
   306   static Register saved_last_sp_register() {
    60   static Register saved_last_sp_register() {
   307     // Should be in sharedRuntime, not here.
    61     // Should be in sharedRuntime, not here.