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