1 /*
   2  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2014, Red Hat Inc. 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.hpp"
  28 #include "assembler_aarch64.inline.hpp"
  29 #include "code/vtableStubs.hpp"
  30 #include "interp_masm_aarch64.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "oops/instanceKlass.hpp"
  33 #include "oops/klassVtable.hpp"
  34 #include "runtime/sharedRuntime.hpp"
  35 #include "vmreg_aarch64.inline.hpp"
  36 #ifdef COMPILER2
  37 #include "opto/runtime.hpp"
  38 #endif
  39 
  40 // machine-dependent part of VtableStubs: create VtableStub of correct size and
  41 // initialize its code
  42 
  43 #define __ masm->
  44 
  45 #ifndef PRODUCT
  46 extern "C" void bad_compiled_vtable_index(JavaThread* thread,
  47                                           oop receiver,
  48                                           int index);
  49 #endif
  50 
  51 VtableStub* VtableStubs::create_vtable_stub(int vtable_index) {
  52   const int aarch64_code_length = VtableStub::pd_code_size_limit(true);
  53   VtableStub* s = new(aarch64_code_length) VtableStub(true, vtable_index);
  54   ResourceMark rm;
  55   CodeBuffer cb(s->entry_point(), aarch64_code_length);
  56   MacroAssembler* masm = new MacroAssembler(&cb);
  57 
  58 #ifndef PRODUCT
  59   if (CountCompiledCalls) {
  60     __ lea(r19, ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr()));
  61     __ incrementw(Address(r19));
  62   }
  63 #endif
  64 
  65   // get receiver (need to skip return address on top of stack)
  66   assert(VtableStub::receiver_location() == j_rarg0->as_VMReg(), "receiver expected in j_rarg0");
  67 
  68   // get receiver klass
  69   address npe_addr = __ pc();
  70   __ load_klass(r19, j_rarg0);
  71 
  72 #ifndef PRODUCT
  73   if (DebugVtables) {
  74     Label L;
  75     // check offset vs vtable length
  76     __ ldrw(rscratch1, Address(r19, InstanceKlass::vtable_length_offset() * wordSize));
  77     __ cmpw(rscratch1, vtable_index * vtableEntry::size());
  78     __ br(Assembler::GT, L);
  79     __ enter();
  80     __ mov(r2, vtable_index);
  81     __ call_VM(noreg,
  82                CAST_FROM_FN_PTR(address, bad_compiled_vtable_index), j_rarg0, r2);
  83     __ leave();
  84     __ bind(L);
  85   }
  86 #endif // PRODUCT
  87 
  88   __ lookup_virtual_method(r19, vtable_index, rmethod);
  89 
  90   if (DebugVtables) {
  91     Label L;
  92     __ cbz(rmethod, L);
  93     __ ldr(rscratch1, Address(rmethod, Method::from_compiled_offset()));
  94     __ cbnz(rscratch1, L);
  95     __ stop("Vtable entry is NULL");
  96     __ bind(L);
  97   }
  98   // r0: receiver klass
  99   // rmethod: Method*
 100   // r2: receiver
 101   address ame_addr = __ pc();
 102   __ ldr(rscratch1, Address(rmethod, Method::from_compiled_offset()));
 103   __ br(rscratch1);
 104 
 105   __ flush();
 106 
 107   if (PrintMiscellaneous && (WizardMode || Verbose)) {
 108     tty->print_cr("vtable #%d at " PTR_FORMAT "[%d] left over: %d",
 109                   vtable_index, p2i(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 
 115   s->set_exception_points(npe_addr, ame_addr);
 116   return s;
 117 }
 118 
 119 
 120 VtableStub* VtableStubs::create_itable_stub(int itable_index) {
 121   // Note well: pd_code_size_limit is the absolute minimum we can get
 122   // away with.  If you add code here, bump the code stub size
 123   // returned by pd_code_size_limit!
 124   const int code_length = VtableStub::pd_code_size_limit(false);
 125   VtableStub* s = new(code_length) VtableStub(false, itable_index);
 126   ResourceMark rm;
 127   CodeBuffer cb(s->entry_point(), code_length);
 128   MacroAssembler* masm = new MacroAssembler(&cb);
 129 
 130 #ifndef PRODUCT
 131   if (CountCompiledCalls) {
 132     __ lea(r10, ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr()));
 133     __ incrementw(Address(r10));
 134   }
 135 #endif
 136 
 137   // Entry arguments:
 138   //  rscratch2: Interface
 139   //  j_rarg0: Receiver
 140 
 141   // Free registers (non-args) are r0 (interface), rmethod
 142 
 143   // get receiver (need to skip return address on top of stack)
 144 
 145   assert(VtableStub::receiver_location() == j_rarg0->as_VMReg(), "receiver expected in j_rarg0");
 146   // get receiver klass (also an implicit null-check)
 147   address npe_addr = __ pc();
 148 
 149   // Most registers are in use; we'll use r0, rmethod, r10, r11
 150   __ load_klass(r10, j_rarg0);
 151 
 152   Label throw_icce;
 153 
 154   // Get Method* and entrypoint for compiler
 155   __ lookup_interface_method(// inputs: rec. class, interface, itable index
 156                              r10, rscratch2, itable_index,
 157                              // outputs: method, scan temp. reg
 158                              rmethod, r11,
 159                              throw_icce);
 160 
 161   // method (rmethod): Method*
 162   // j_rarg0: receiver
 163 
 164 #ifdef ASSERT
 165   if (DebugVtables) {
 166     Label L2;
 167     __ cbz(rmethod, L2);
 168     __ ldr(rscratch1, Address(rmethod, Method::from_compiled_offset()));
 169     __ cbnz(rscratch1, L2);
 170     __ stop("compiler entrypoint is null");
 171     __ bind(L2);
 172   }
 173 #endif // ASSERT
 174 
 175   // rmethod: Method*
 176   // j_rarg0: receiver
 177   address ame_addr = __ pc();
 178   __ ldr(rscratch1, Address(rmethod, Method::from_compiled_offset()));
 179   __ br(rscratch1);
 180 
 181   __ bind(throw_icce);
 182   __ far_jump(RuntimeAddress(StubRoutines::throw_IncompatibleClassChangeError_entry()));
 183 
 184   __ flush();
 185 
 186   if (PrintMiscellaneous && (WizardMode || Verbose)) {
 187     tty->print_cr("itable #%d at " PTR_FORMAT "[%d] left over: %d",
 188                   itable_index, p2i(s->entry_point()),
 189                   (int)(s->code_end() - s->entry_point()),
 190                   (int)(s->code_end() - __ pc()));
 191   }
 192   guarantee(__ pc() <= s->code_end(), "overflowed buffer");
 193 
 194   s->set_exception_points(npe_addr, ame_addr);
 195   return s;
 196 }
 197 
 198 
 199 int VtableStub::pd_code_size_limit(bool is_vtable_stub) {
 200   int size = DebugVtables ? 216 : 0;
 201   if (CountCompiledCalls)
 202     size += 6 * 4;
 203   // FIXME
 204   if (is_vtable_stub)
 205     size += 52;
 206   else
 207     size += 104;
 208   return size;
 209 
 210   // In order to tune these parameters, run the JVM with VM options
 211   // +PrintMiscellaneous and +WizardMode to see information about
 212   // actual itable stubs.  Run it with -Xmx31G -XX:+UseCompressedOops.
 213   //
 214   // If Universe::narrow_klass_base is nonzero, decoding a compressed
 215   // class can take zeveral instructions.  Run it with -Xmx31G
 216   // -XX:+UseCompressedOops.
 217   //
 218   // The JVM98 app. _202_jess has a megamorphic interface call.
 219   // The itable code looks like this:
 220   // Decoding VtableStub itbl[1]@12
 221   //     ldr     w10, [x1,#8]
 222   //     lsl     x10, x10, #3
 223   //     ldr     w11, [x10,#280]
 224   //     add     x11, x10, x11, uxtx #3
 225   //     add     x11, x11, #0x1b8
 226   //     ldr     x12, [x11]
 227   //     cmp     x9, x12
 228   //     b.eq    success
 229   // loop:
 230   //     cbz     x12, throw_icce
 231   //     add     x11, x11, #0x10
 232   //     ldr     x12, [x11]
 233   //     cmp     x9, x12
 234   //     b.ne    loop
 235   // success:
 236   //     ldr     x11, [x11,#8]
 237   //     ldr     x12, [x10,x11]
 238   //     ldr     x8, [x12,#72]
 239   //     br      x8
 240   // throw_icce:
 241   //     b      throw_ICCE_entry
 242 
 243 }
 244 
 245 int VtableStub::pd_code_alignment() { return 4; }