1 /*
   2  * Copyright (c) 1997, 2013, 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 "code/vtableStubs.hpp"
  28 #include "interp_masm_sparc.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "oops/instanceKlass.hpp"
  31 #include "oops/klassVtable.hpp"
  32 #include "runtime/sharedRuntime.hpp"
  33 #include "vmreg_sparc.inline.hpp"
  34 #ifdef COMPILER2
  35 #include "opto/runtime.hpp"
  36 #endif
  37 
  38 // machine-dependent part of VtableStubs: create vtableStub of correct size and
  39 // initialize its code
  40 
  41 #define __ masm->
  42 
  43 
  44 #ifndef PRODUCT
  45 extern "C" void bad_compiled_vtable_index(JavaThread* thread, oopDesc* receiver, int index);
  46 #endif
  47 
  48 
  49 // Used by compiler only; may use only caller saved, non-argument registers
  50 // NOTE:  %%%% if any change is made to this stub make sure that the function
  51 //             pd_code_size_limit is changed to ensure the correct size for VtableStub
  52 VtableStub* VtableStubs::create_vtable_stub(int vtable_index) {
  53   const int sparc_code_length = VtableStub::pd_code_size_limit(true);
  54   VtableStub* s = new(sparc_code_length) VtableStub(true, vtable_index);
  55   ResourceMark rm;
  56   CodeBuffer cb(s->entry_point(), sparc_code_length);
  57   MacroAssembler* masm = new MacroAssembler(&cb);
  58 
  59 #ifndef PRODUCT
  60   if (CountCompiledCalls) {
  61     __ inc_counter(SharedRuntime::nof_megamorphic_calls_addr(), G5, G3_scratch);
  62   }
  63 #endif /* PRODUCT */
  64 
  65   assert(VtableStub::receiver_location() == O0->as_VMReg(), "receiver expected in O0");
  66 
  67   // get receiver klass
  68   address npe_addr = __ pc();
  69   __ load_klass(O0, G3_scratch);
  70 
  71   // set Method* (in case of interpreted method), and destination address
  72 #ifndef PRODUCT
  73   if (DebugVtables) {
  74     Label L;
  75     // check offset vs vtable length
  76     __ ld(G3_scratch, InstanceKlass::vtable_length_offset()*wordSize, G5);
  77     __ cmp_and_br_short(G5, vtable_index*vtableEntry::size(), Assembler::greaterUnsigned, Assembler::pt, L);
  78     __ set(vtable_index, O2);
  79     __ call_VM(noreg, CAST_FROM_FN_PTR(address, bad_compiled_vtable_index), O0, O2);
  80     __ bind(L);
  81   }
  82 #endif
  83 
  84   __ lookup_virtual_method(G3_scratch, vtable_index, G5_method);
  85 
  86 #ifndef PRODUCT
  87   if (DebugVtables) {
  88     Label L;
  89     __ br_notnull_short(G5_method, Assembler::pt, L);
  90     __ stop("Vtable entry is ZERO");
  91     __ bind(L);
  92   }
  93 #endif
  94 
  95   address ame_addr = __ pc();  // if the vtable entry is null, the method is abstract
  96                                // NOTE: for vtable dispatches, the vtable entry will never be null.
  97 
  98   __ ld_ptr(G5_method, in_bytes(Method::from_compiled_offset()), G3_scratch);
  99 
 100   // jump to target (either compiled code or c2iadapter)
 101   __ JMP(G3_scratch, 0);
 102   // load Method* (in case we call c2iadapter)
 103   __ delayed()->nop();
 104 
 105   masm->flush();
 106 
 107   if (PrintMiscellaneous && (WizardMode || Verbose)) {
 108     tty->print_cr("vtable #%d at "PTR_FORMAT"[%d] left over: %d",
 109                   vtable_index, s->entry_point(),
 110                   (int)(s->code_end() - s->entry_point()),
 111                   (int)(s->code_end() - __ pc()));
 112   }
 113   guarantee(__ pc() <= s->code_end(), "overflowed buffer");
 114   // shut the door on sizing bugs
 115   int slop = 2*BytesPerInstWord;  // 32-bit offset is this much larger than a 13-bit one
 116   assert(vtable_index > 10 || __ pc() + slop <= s->code_end(), "room for sethi;add");
 117 
 118   s->set_exception_points(npe_addr, ame_addr);
 119   return s;
 120 }
 121 
 122 
 123 // NOTE:  %%%% if any change is made to this stub make sure that the function
 124 //             pd_code_size_limit is changed to ensure the correct size for VtableStub
 125 VtableStub* VtableStubs::create_itable_stub(int itable_index) {
 126   const int sparc_code_length = VtableStub::pd_code_size_limit(false);
 127   VtableStub* s = new(sparc_code_length) VtableStub(false, itable_index);
 128   ResourceMark rm;
 129   CodeBuffer cb(s->entry_point(), sparc_code_length);
 130   MacroAssembler* masm = new MacroAssembler(&cb);
 131 
 132   Register G3_Klass = G3_scratch;
 133   Register G5_interface = G5;  // Passed in as an argument
 134   Label search;
 135 
 136   // Entry arguments:
 137   //  G5_interface: Interface
 138   //  O0:           Receiver
 139   assert(VtableStub::receiver_location() == O0->as_VMReg(), "receiver expected in O0");
 140 
 141   // get receiver klass (also an implicit null-check)
 142   address npe_addr = __ pc();
 143   __ load_klass(O0, G3_Klass);
 144 
 145   // Push a new window to get some temp registers.  This chops the head of all
 146   // my 64-bit %o registers in the LION build, but this is OK because no longs
 147   // are passed in the %o registers.  Instead, longs are passed in G1 and G4
 148   // and so those registers are not available here.
 149   __ save(SP,-frame::register_save_words*wordSize,SP);
 150 
 151 #ifndef PRODUCT
 152   if (CountCompiledCalls) {
 153     __ inc_counter(SharedRuntime::nof_megamorphic_calls_addr(), L0, L1);
 154   }
 155 #endif /* PRODUCT */
 156 
 157   Label throw_icce;
 158 
 159   Register L5_method = L5;
 160   __ lookup_interface_method(// inputs: rec. class, interface, itable index
 161                              G3_Klass, G5_interface, itable_index,
 162                              // outputs: method, scan temp. reg
 163                              L5_method, L2, L3,
 164                              throw_icce);
 165 
 166 #ifndef PRODUCT
 167   if (DebugVtables) {
 168     Label L01;
 169     __ br_notnull_short(L5_method, Assembler::pt, L01);
 170     __ stop("Method* is null");
 171     __ bind(L01);
 172   }
 173 #endif
 174 
 175   // If the following load is through a NULL pointer, we'll take an OS
 176   // exception that should translate into an AbstractMethodError.  We need the
 177   // window count to be correct at that time.
 178   __ restore(L5_method, 0, G5_method);
 179   // Restore registers *before* the AME point.
 180 
 181   address ame_addr = __ pc();   // if the vtable entry is null, the method is abstract
 182   __ ld_ptr(G5_method, in_bytes(Method::from_compiled_offset()), G3_scratch);
 183 
 184   // G5_method:  Method*
 185   // O0:         Receiver
 186   // G3_scratch: entry point
 187   __ JMP(G3_scratch, 0);
 188   __ delayed()->nop();
 189 
 190   __ bind(throw_icce);
 191   AddressLiteral icce(StubRoutines::throw_IncompatibleClassChangeError_entry());
 192   __ jump_to(icce, G3_scratch);
 193   __ delayed()->restore();
 194 
 195   masm->flush();
 196 
 197   if (PrintMiscellaneous && (WizardMode || Verbose)) {
 198     tty->print_cr("itable #%d at "PTR_FORMAT"[%d] left over: %d",
 199                   itable_index, s->entry_point(),
 200                   (int)(s->code_end() - s->entry_point()),
 201                   (int)(s->code_end() - __ pc()));
 202   }
 203   guarantee(__ pc() <= s->code_end(), "overflowed buffer");
 204   // shut the door on sizing bugs
 205   int slop = 2*BytesPerInstWord;  // 32-bit offset is this much larger than a 13-bit one
 206   assert(itable_index > 10 || __ pc() + slop <= s->code_end(), "room for sethi;add");
 207 
 208   s->set_exception_points(npe_addr, ame_addr);
 209   return s;
 210 }
 211 
 212 
 213 int VtableStub::pd_code_size_limit(bool is_vtable_stub) {
 214   if (TraceJumps || DebugVtables || CountCompiledCalls || VerifyOops) return 1000;
 215   else {
 216     const int slop = 2*BytesPerInstWord; // sethi;add  (needed for long offsets)
 217     if (is_vtable_stub) {
 218       // ld;ld;ld,jmp,nop
 219       const int basic = 5*BytesPerInstWord +
 220                         // shift;add for load_klass (only shift with zero heap based)
 221                         (UseCompressedClassPointers ?
 222                           MacroAssembler::instr_size_for_decode_klass_not_null() : 0);
 223       return basic + slop;
 224     } else {
 225       const int basic = (28 LP64_ONLY(+ 6)) * BytesPerInstWord +
 226                         // shift;add for load_klass (only shift with zero heap based)
 227                         (UseCompressedClassPointers ?
 228                           MacroAssembler::instr_size_for_decode_klass_not_null() : 0);
 229       return (basic + slop);
 230     }
 231   }
 232 
 233   // In order to tune these parameters, run the JVM with VM options
 234   // +PrintMiscellaneous and +WizardMode to see information about
 235   // actual itable stubs.  Look for lines like this:
 236   //   itable #1 at 0x5551212[116] left over: 8
 237   // Reduce the constants so that the "left over" number is 8
 238   // Do not aim at a left-over number of zero, because a very
 239   // large vtable or itable offset (> 4K) will require an extra
 240   // sethi/or pair of instructions.
 241   //
 242   // The JVM98 app. _202_jess has a megamorphic interface call.
 243   // The itable code looks like this:
 244   // Decoding VtableStub itbl[1]@16
 245   //   ld  [ %o0 + 4 ], %g3
 246   //   save  %sp, -64, %sp
 247   //   ld  [ %g3 + 0xe8 ], %l2
 248   //   sll  %l2, 2, %l2
 249   //   add  %l2, 0x134, %l2
 250   //   and  %l2, -8, %l2        ! NOT_LP64 only
 251   //   add  %g3, %l2, %l2
 252   //   add  %g3, 4, %g3
 253   //   ld  [ %l2 ], %l5
 254   //   brz,pn   %l5, throw_icce
 255   //   cmp  %l5, %g5
 256   //   be  %icc, success
 257   //   add  %l2, 8, %l2
 258   // loop:
 259   //   ld  [ %l2 ], %l5
 260   //   brz,pn   %l5, throw_icce
 261   //   cmp  %l5, %g5
 262   //   bne,pn   %icc, loop
 263   //   add  %l2, 8, %l2
 264   // success:
 265   //   ld  [ %l2 + -4 ], %l2
 266   //   ld  [ %g3 + %l2 ], %l5
 267   //   restore  %l5, 0, %g5
 268   //   ld  [ %g5 + 0x44 ], %g3
 269   //   jmp  %g3
 270   //   nop
 271   // throw_icce:
 272   //   sethi  %hi(throw_ICCE_entry), %g3
 273   //   ! 5 more instructions here, LP64_ONLY
 274   //   jmp  %g3 + %lo(throw_ICCE_entry)
 275   //   restore
 276 }
 277 
 278 
 279 int VtableStub::pd_code_alignment() {
 280   // UltraSPARC cache line size is 8 instructions:
 281   const unsigned int icache_line_size = 32;
 282   return icache_line_size;
 283 }