1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2012, 2015 SAP AG. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "code/vtableStubs.hpp"
  29 #include "interp_masm_ppc_64.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "oops/instanceKlass.hpp"
  32 #include "oops/klassVtable.hpp"
  33 #include "runtime/sharedRuntime.hpp"
  34 #include "vmreg_ppc.inline.hpp"
  35 #ifdef COMPILER2
  36 #include "opto/runtime.hpp"
  37 #endif
  38 
  39 #define __ masm->
  40 
  41 #ifdef PRODUCT
  42 #define BLOCK_COMMENT(str) // nothing
  43 #else
  44 #define BLOCK_COMMENT(str) __ block_comment(str)
  45 #endif
  46 #define BIND(label) bind(label); BLOCK_COMMENT(#label ":")
  47 
  48 #ifndef PRODUCT
  49 extern "C" void bad_compiled_vtable_index(JavaThread* thread, oopDesc* receiver, int index);
  50 #endif
  51 
  52 // Used by compiler only; may use only caller saved, non-argument
  53 // registers.
  54 VtableStub* VtableStubs::create_vtable_stub(int vtable_index) {
  55   // PPC port: use fixed size.
  56   const int code_length = VtableStub::pd_code_size_limit(true);
  57   VtableStub* s = new (code_length) VtableStub(true, vtable_index);
  58   ResourceMark rm;
  59   CodeBuffer cb(s->entry_point(), code_length);
  60   MacroAssembler* masm = new MacroAssembler(&cb);
  61   address start_pc;
  62 
  63 #ifndef PRODUCT
  64   if (CountCompiledCalls) {
  65     __ load_const(R11_scratch1, SharedRuntime::nof_megamorphic_calls_addr());
  66     __ lwz(R12_scratch2, 0, R11_scratch1);
  67     __ addi(R12_scratch2, R12_scratch2, 1);
  68     __ stw(R12_scratch2, 0, R11_scratch1);
  69   }
  70 #endif
  71 
  72   assert(VtableStub::receiver_location() == R3_ARG1->as_VMReg(), "receiver expected in R3_ARG1");
  73 
  74   // Get receiver klass.
  75   const Register rcvr_klass = R11_scratch1;
  76 
  77   // We might implicit NULL fault here.
  78   address npe_addr = __ pc(); // npe = null pointer exception
  79   __ load_klass_with_trap_null_check(rcvr_klass, R3);
  80 
  81  // Set method (in case of interpreted method), and destination address.
  82   int entry_offset = InstanceKlass::vtable_start_offset() + vtable_index*vtableEntry::size();
  83 
  84 #ifndef PRODUCT
  85   if (DebugVtables) {
  86     Label L;
  87     // Check offset vs vtable length.
  88     const Register vtable_len = R12_scratch2;
  89     __ lwz(vtable_len, InstanceKlass::vtable_length_offset()*wordSize, rcvr_klass);
  90     __ cmpwi(CCR0, vtable_len, vtable_index*vtableEntry::size());
  91     __ bge(CCR0, L);
  92     __ li(R12_scratch2, vtable_index);
  93     __ call_VM(noreg, CAST_FROM_FN_PTR(address, bad_compiled_vtable_index), R3_ARG1, R12_scratch2, false);
  94     __ bind(L);
  95   }
  96 #endif
  97 
  98   int v_off = entry_offset*wordSize + vtableEntry::method_offset_in_bytes();
  99 
 100   __ ld(R19_method, v_off, rcvr_klass);
 101 
 102 #ifndef PRODUCT
 103   if (DebugVtables) {
 104     Label L;
 105     __ cmpdi(CCR0, R19_method, 0);
 106     __ bne(CCR0, L);
 107     __ stop("Vtable entry is ZERO", 102);
 108     __ bind(L);
 109   }
 110 #endif
 111 
 112   // If the vtable entry is null, the method is abstract.
 113   address ame_addr = __ pc(); // ame = abstract method error
 114 
 115   __ load_with_trap_null_check(R12_scratch2, in_bytes(Method::from_compiled_offset()), R19_method);
 116   __ mtctr(R12_scratch2);
 117   __ bctr();
 118   masm->flush();
 119 
 120   guarantee(__ pc() <= s->code_end(), "overflowed buffer");
 121 
 122   s->set_exception_points(npe_addr, ame_addr);
 123 
 124   return s;
 125 }
 126 
 127 VtableStub* VtableStubs::create_itable_stub(int vtable_index) {
 128   // PPC port: use fixed size.
 129   const int code_length = VtableStub::pd_code_size_limit(false);
 130   VtableStub* s = new (code_length) VtableStub(false, vtable_index);
 131   ResourceMark rm;
 132   CodeBuffer cb(s->entry_point(), code_length);
 133   MacroAssembler* masm = new MacroAssembler(&cb);
 134   address start_pc;
 135 
 136 #ifndef PRODUCT
 137   if (CountCompiledCalls) {
 138     __ load_const(R11_scratch1, SharedRuntime::nof_megamorphic_calls_addr());
 139     __ lwz(R12_scratch2, 0, R11_scratch1);
 140     __ addi(R12_scratch2, R12_scratch2, 1);
 141     __ stw(R12_scratch2, 0, R11_scratch1);
 142   }
 143 #endif
 144 
 145   assert(VtableStub::receiver_location() == R3_ARG1->as_VMReg(), "receiver expected in R3_ARG1");
 146 
 147   // Entry arguments:
 148   //  R19_method: Interface
 149   //  R3_ARG1:    Receiver
 150   //
 151 
 152   const Register rcvr_klass = R11_scratch1;
 153   const Register vtable_len = R12_scratch2;
 154   const Register itable_entry_addr = R21_tmp1;
 155   const Register itable_interface = R22_tmp2;
 156 
 157   // Get receiver klass.
 158 
 159   // We might implicit NULL fault here.
 160   address npe_addr = __ pc(); // npe = null pointer exception
 161   __ load_klass_with_trap_null_check(rcvr_klass, R3_ARG1);
 162 
 163   BLOCK_COMMENT("Load start of itable entries into itable_entry.");
 164   __ lwz(vtable_len, InstanceKlass::vtable_length_offset() * wordSize, rcvr_klass);
 165   __ slwi(vtable_len, vtable_len, exact_log2(vtableEntry::size() * wordSize));
 166   __ add(itable_entry_addr, vtable_len, rcvr_klass);
 167 
 168   // Loop over all itable entries until desired interfaceOop(Rinterface) found.
 169   BLOCK_COMMENT("Increment itable_entry_addr in loop.");
 170   const int vtable_base_offset = InstanceKlass::vtable_start_offset() * wordSize;
 171   __ addi(itable_entry_addr, itable_entry_addr, vtable_base_offset + itableOffsetEntry::interface_offset_in_bytes());
 172 
 173   const int itable_offset_search_inc = itableOffsetEntry::size() * wordSize;
 174   Label search;
 175   __ bind(search);
 176   __ ld(itable_interface, 0, itable_entry_addr);
 177 
 178   // Handle IncompatibleClassChangeError in itable stubs.
 179   // If the entry is NULL then we've reached the end of the table
 180   // without finding the expected interface, so throw an exception.
 181   BLOCK_COMMENT("Handle IncompatibleClassChangeError in itable stubs.");
 182   Label throw_icce;
 183   __ cmpdi(CCR1, itable_interface, 0);
 184   __ cmpd(CCR0, itable_interface, R19_method);
 185   __ addi(itable_entry_addr, itable_entry_addr, itable_offset_search_inc);
 186   __ beq(CCR1, throw_icce);
 187   __ bne(CCR0, search);
 188 
 189   // Entry found and itable_entry_addr points to it, get offset of vtable for interface.
 190 
 191   const Register vtable_offset = R12_scratch2;
 192   const Register itable_method = R11_scratch1;
 193 
 194   const int vtable_offset_offset = (itableOffsetEntry::offset_offset_in_bytes() -
 195                                     itableOffsetEntry::interface_offset_in_bytes()) -
 196                                    itable_offset_search_inc;
 197   __ lwz(vtable_offset, vtable_offset_offset, itable_entry_addr);
 198 
 199   // Compute itableMethodEntry and get method and entry point for compiler.
 200   const int method_offset = (itableMethodEntry::size() * wordSize * vtable_index) +
 201     itableMethodEntry::method_offset_in_bytes();
 202 
 203   __ add(itable_method, rcvr_klass, vtable_offset);
 204   __ ld(R19_method, method_offset, itable_method);
 205 
 206 #ifndef PRODUCT
 207   if (DebugVtables) {
 208     Label ok;
 209     __ cmpd(CCR0, R19_method, 0);
 210     __ bne(CCR0, ok);
 211     __ stop("method is null", 103);
 212     __ bind(ok);
 213   }
 214 #endif
 215 
 216   // If the vtable entry is null, the method is abstract.
 217   address ame_addr = __ pc(); // ame = abstract method error
 218 
 219   // Must do an explicit check if implicit checks are disabled.
 220   assert(!MacroAssembler::needs_explicit_null_check(in_bytes(Method::from_compiled_offset())), "sanity");
 221   if (!ImplicitNullChecks || !os::zero_page_read_protected()) {
 222     if (TrapBasedNullChecks) {
 223       __ trap_null_check(R19_method);
 224     } else {
 225       __ cmpdi(CCR0, R19_method, 0);
 226       __ beq(CCR0, throw_icce);
 227     }
 228   }
 229   __ ld(R12_scratch2, in_bytes(Method::from_compiled_offset()), R19_method);
 230   __ mtctr(R12_scratch2);
 231   __ bctr();
 232 
 233   // Handle IncompatibleClassChangeError in itable stubs.
 234   // More detailed error message.
 235   // We force resolving of the call site by jumping to the "handle
 236   // wrong method" stub, and so let the interpreter runtime do all the
 237   // dirty work.
 238   __ bind(throw_icce);
 239   __ load_const(R11_scratch1, SharedRuntime::get_handle_wrong_method_stub());
 240   __ mtctr(R11_scratch1);
 241   __ bctr();
 242 
 243   masm->flush();
 244 
 245   guarantee(__ pc() <= s->code_end(), "overflowed buffer");
 246 
 247   s->set_exception_points(npe_addr, ame_addr);
 248   return s;
 249 }
 250 
 251 int VtableStub::pd_code_size_limit(bool is_vtable_stub) {
 252   if (TraceJumps || DebugVtables || CountCompiledCalls || VerifyOops) {
 253     return 1000;
 254   } else {
 255     int decode_klass_size = MacroAssembler::instr_size_for_decode_klass_not_null();
 256     if (is_vtable_stub) {
 257       return 20 + decode_klass_size +  8 + 8;   // Plain + cOops + Traps + safety
 258     } else {
 259       return 96 + decode_klass_size + 12 + 8;   // Plain + cOops + Traps + safety
 260     }
 261   }
 262 }
 263 
 264 int VtableStub::pd_code_alignment() {
 265   const unsigned int icache_line_size = 32;
 266   return icache_line_size;
 267 }