1 /*
   2  * Copyright (c) 2004, 2012, 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 #include "precompiled.hpp"
  26 #include "asm/macroAssembler.inline.hpp"
  27 #include "asm/codeBuffer.hpp"
  28 #include "memory/metaspaceShared.hpp"
  29 
  30 // Generate the self-patching vtable method:
  31 //
  32 // This method will be called (as any other Klass virtual method) with
  33 // the Klass itself as the first argument.  Example:
  34 //
  35 //      oop obj;
  36 //      int size = obj->klass()->oop_size(this);
  37 //
  38 // for which the virtual method call is Klass::oop_size();
  39 //
  40 // The dummy method is called with the Klass object as the first
  41 // operand, and an object as the second argument.
  42 //
  43 
  44 //=====================================================================
  45 
  46 // All of the dummy methods in the vtable are essentially identical,
  47 // differing only by an ordinal constant, and they bear no relationship
  48 // to the original method which the caller intended. Also, there needs
  49 // to be 'vtbl_list_size' instances of the vtable in order to
  50 // differentiate between the 'vtable_list_size' original Klass objects.
  51 
  52 #define __ masm->
  53 
  54 void MetaspaceShared::generate_vtable_methods(void** vtbl_list,
  55                                                    void** vtable,
  56                                                    char** md_top,
  57                                                    char* md_end,
  58                                                    char** mc_top,
  59                                                    char* mc_end) {
  60 
  61   intptr_t vtable_bytes = (num_virtuals * vtbl_list_size) * sizeof(void*);
  62   *(intptr_t *)(*md_top) = vtable_bytes;
  63   *md_top += sizeof(intptr_t);
  64   void** dummy_vtable = (void**)*md_top;
  65   *vtable = dummy_vtable;
  66   *md_top += vtable_bytes;
  67 
  68   // Get ready to generate dummy methods.
  69 
  70   CodeBuffer cb((unsigned char*)*mc_top, mc_end - *mc_top);
  71   MacroAssembler* masm = new MacroAssembler(&cb);
  72 
  73   Label common_code;
  74   for (int i = 0; i < vtbl_list_size; ++i) {
  75     for (int j = 0; j < num_virtuals; ++j) {
  76       dummy_vtable[num_virtuals * i + j] = (void*)masm->pc();
  77       __ save(SP, -256, SP);
  78       int offset = (i << 8) + j;
  79       Register src = G0;
  80       if (!Assembler::is_simm13(offset)) {
  81         __ sethi(offset, L0);
  82         src = L0;
  83         offset = offset & ((1 << 10) - 1);
  84       }
  85       __ brx(Assembler::always, false, Assembler::pt, common_code);
  86 
  87       // Load L0 with a value indicating vtable/offset pair.
  88       // -- bits[ 7..0]  (8 bits) which virtual method in table?
  89       // -- bits[13..8]  (6 bits) which virtual method table?
  90       __ delayed()->or3(src, offset, L0);
  91     }
  92   }
  93 
  94   __ bind(common_code);
  95 
  96   // Expecting to be called with the "this" pointer in O0/I0 (where
  97   // "this" is a Klass object).  In addition, L0 was set (above) to
  98   // identify the method and table.
  99 
 100   // Look up the correct vtable pointer.
 101 
 102   __ set((intptr_t)vtbl_list, L2);      // L2 = address of new vtable list.
 103   __ srl(L0, 8, L3);                    // Isolate L3 = vtable identifier.
 104   __ sll(L3, LogBytesPerWord, L3);
 105   __ ld_ptr(L2, L3, L3);                // L3 = new (correct) vtable pointer.
 106   __ st_ptr(L3, Address(I0, 0));        // Save correct vtable ptr in entry.
 107 
 108   // Restore registers and jump to the correct method;
 109 
 110   __ and3(L0, 255, L4);                 // Isolate L3 = method offset;.
 111   __ sll(L4, LogBytesPerWord, L4);
 112   __ ld_ptr(L3, L4, L4);                // Get address of correct virtual method
 113   __ jmpl(L4, 0, G0);                   // Jump to correct method.
 114   __ delayed()->restore();              // Restore registers.
 115 
 116   __ flush();
 117   *mc_top = (char*)__ pc();
 118 
 119   guarantee(*mc_top <= mc_end, "Insufficient space for method wrappers.");
 120 }