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  * Copyright (c) 2015, Linaro Ltd. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  *
  25  */
  26 
  27 #include "precompiled.hpp"
  28 #include "asm/macroAssembler.hpp"
  29 #include "assembler_aarch32.inline.hpp"
  30 #include "code/vtableStubs.hpp"
  31 #include "interp_masm_aarch32.hpp"
  32 #include "memory/resourceArea.hpp"
  33 #include "oops/compiledICHolder.hpp"
  34 #include "oops/instanceKlass.hpp"
  35 #include "oops/klassVtable.hpp"
  36 #include "runtime/sharedRuntime.hpp"
  37 #include "vmreg_aarch32.inline.hpp"
  38 #ifdef COMPILER2
  39 #include "opto/runtime.hpp"
  40 #endif
  41 
  42 // machine-dependent part of VtableStubs: create VtableStub of correct size and
  43 // initialize its code
  44 
  45 #define __ masm->
  46 
  47 #ifndef PRODUCT
  48 extern "C" void bad_compiled_vtable_index(JavaThread* thread,
  49                                           oop receiver,
  50                                           int index);
  51 #endif
  52 
  53 VtableStub* VtableStubs::create_vtable_stub(int vtable_index) {
  54   const int aarch32_code_length = VtableStub::pd_code_size_limit(true);
  55   VtableStub* s = new(aarch32_code_length) VtableStub(true, vtable_index);
  56   // Can be NULL if there is no free space in the code cache.
  57   if (s == NULL) {
  58     return NULL;
  59   }
  60 
  61   ResourceMark rm;
  62   CodeBuffer cb(s->entry_point(), aarch32_code_length);
  63   MacroAssembler* masm = new MacroAssembler(&cb);
  64 
  65 #ifndef PRODUCT
  66   if (CountCompiledCalls) {
  67     // FIXME SharedRuntime::nof_megamorphic_calls_addr() returns un-encodable address
  68     __ increment(ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr()), 1);
  69   }
  70 #endif
  71 
  72   // get receiver (need to skip return address on top of stack)
  73   assert(VtableStub::receiver_location() == j_rarg0->as_VMReg(), "receiver expected in j_rarg0");
  74 
  75   // get receiver klass
  76   address npe_addr = __ pc();
  77   __ load_klass(rscratch2, j_rarg0);
  78 
  79 #ifndef PRODUCT
  80   if (DebugVtables) {
  81     Label L;
  82     // check offset vs vtable length
  83     __ ldr(rscratch1, Address(rscratch2, InstanceKlass::vtable_length_offset() * wordSize));
  84     __ cmp(rscratch1, vtable_index * vtableEntry::size());
  85     __ b(L, Assembler::GT);
  86     __ enter();
  87     __ mov(r2, vtable_index);
  88     __ call_VM(noreg,
  89                CAST_FROM_FN_PTR(address, bad_compiled_vtable_index), j_rarg0, r2);
  90     __ leave();
  91     __ bind(L);
  92   }
  93 #endif // PRODUCT
  94 
  95   __ lookup_virtual_method(rscratch2, vtable_index, rmethod);
  96 
  97   if (DebugVtables) {
  98     Label L;
  99     __ cbz(rmethod, L);
 100     __ ldr(rscratch1, Address(rmethod, Method::from_compiled_offset()));
 101     __ cbnz(rscratch1, L);
 102     __ stop("Vtable entry is NULL");
 103     __ bind(L);
 104   }
 105   // r0: receiver klass
 106   // rmethod: Method*
 107   // r2: receiver
 108   address ame_addr = __ pc();
 109   __ ldr(rscratch1, Address(rmethod, Method::from_compiled_offset()));
 110   __ b(rscratch1);
 111 
 112   __ flush();
 113 
 114   if (PrintMiscellaneous && (WizardMode || Verbose)) {
 115     tty->print_cr("vtable #%d at " PTR_FORMAT "[%d] left over: %d",
 116                   vtable_index, p2i(s->entry_point()),
 117                   (int)(s->code_end() - s->entry_point()),
 118                   (int)(s->code_end() - __ pc()));
 119   }
 120   guarantee(__ pc() <= s->code_end(), "overflowed buffer");
 121 
 122   s->set_exception_points(npe_addr, ame_addr);
 123   return s;
 124 }
 125 
 126 
 127 VtableStub* VtableStubs::create_itable_stub(int itable_index) {
 128   // Note well: pd_code_size_limit is the absolute minimum we can get
 129   // away with.  If you add code here, bump the code stub size
 130   // returned by pd_code_size_limit!
 131   const int code_length = VtableStub::pd_code_size_limit(false);
 132   VtableStub* s = new(code_length) VtableStub(false, itable_index);
 133   // Can be NULL if there is no free space in the code cache.
 134   if (s == NULL) {
 135     return NULL;
 136   }
 137 
 138   ResourceMark rm;
 139   CodeBuffer cb(s->entry_point(), code_length);
 140   MacroAssembler* masm = new MacroAssembler(&cb);
 141 
 142 #ifndef PRODUCT
 143   if (CountCompiledCalls) {
 144     // FIXME SharedRuntime::nof_megamorphic_calls_addr() returns un-encodable address
 145     __ increment(ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr()), 1);
 146   }
 147 #endif
 148 
 149   // Entry arguments:
 150   //  rscratch2: CompiledICHolder
 151   //  j_rarg0: Receiver
 152 
 153   // Free registers (non-args) are r0 (interface), rmethod
 154 
 155   // Most registers are in use; we'll use r0, rmethod, rscratch1, r4
 156   // IMPORTANT: r4 is used as a temp register, if it's changed callee-save
 157   // the code should be fixed
 158   // TODO: put an assert here to ensure r4 is caller-save
 159   const Register recv_klass_reg     = rscratch1;
 160   const Register holder_klass_reg   = rscratch2; // declaring interface klass (DECC)
 161   const Register resolved_klass_reg = rmethod; // resolved interface klass (REFC)
 162   const Register temp_reg           = r4;
 163 
 164   __ ldr(resolved_klass_reg, Address(rscratch2, CompiledICHolder::holder_klass_offset()));
 165   __ ldr(holder_klass_reg,   Address(rscratch2, CompiledICHolder::holder_metadata_offset()));
 166 
 167   Label L_no_such_interface;
 168 
 169   // get receiver klass (also an implicit null-check)
 170   address npe_addr = __ pc();
 171   assert(VtableStub::receiver_location() == j_rarg0->as_VMReg(), "receiver expected in j_rarg0");
 172   __ load_klass(recv_klass_reg, j_rarg0);
 173 
 174   // Receiver subtype check against REFC.
 175   // Destroys recv_klass_reg value.
 176   __ lookup_interface_method(// inputs: rec. class, interface
 177                              recv_klass_reg, resolved_klass_reg, noreg,
 178                              // outputs:  scan temp. reg1, scan temp. reg2
 179                              recv_klass_reg, temp_reg,
 180                              L_no_such_interface,
 181                              /*return_method=*/false);
 182 
 183   // Get selected method from declaring class and itable index
 184   __ load_klass(recv_klass_reg, j_rarg0); // restore recv_klass_reg
 185   __ lookup_interface_method(// inputs: rec. class, interface, itable index
 186                              recv_klass_reg, holder_klass_reg, itable_index,
 187                              // outputs: method, scan temp. reg
 188                              rmethod, temp_reg,
 189                              L_no_such_interface);
 190   // rmethod: Method*
 191   // j_rarg0: receiver
 192 
 193 #ifdef ASSERT
 194   if (DebugVtables) {
 195     Label L2;
 196     __ cbz(rmethod, L2);
 197     __ ldr(recv_klass_reg, Address(rmethod, Method::from_compiled_offset()));
 198     __ cbnz(recv_klass_reg, L2);
 199     __ stop("compiler entrypoint is null");
 200     __ bind(L2);
 201   }
 202 #endif // ASSERT
 203 
 204   address ame_addr = __ pc();
 205   __ ldr(recv_klass_reg, Address(rmethod, Method::from_compiled_offset()));
 206   __ b(recv_klass_reg);
 207 
 208   __ bind(L_no_such_interface);
 209   __ far_jump(RuntimeAddress(StubRoutines::throw_IncompatibleClassChangeError_entry()));
 210 
 211   __ flush();
 212 
 213   if (PrintMiscellaneous && (WizardMode || Verbose)) {
 214     tty->print_cr("itable #%d at "PTR_FORMAT"[%d] left over: %d",
 215                   itable_index, p2i(s->entry_point()),
 216                   (int)(s->code_end() - s->entry_point()),
 217                   (int)(s->code_end() - __ pc()));
 218   }
 219   guarantee(__ pc() <= s->code_end(), "overflowed buffer");
 220 
 221   s->set_exception_points(npe_addr, ame_addr);
 222   return s;
 223 }
 224 
 225 
 226 int VtableStub::pd_code_size_limit(bool is_vtable_stub) {
 227   int size = DebugVtables ? 216 : 0; // FIXME
 228   if (CountCompiledCalls)
 229     size += 6 * 4; // FIXME. cannot measure, CountCalls does not work
 230   if (is_vtable_stub) {
 231     size += 26;
 232   } else {
 233     size += 160;
 234     if (!(VM_Version::features() & (FT_ARMV7 | FT_ARMV6T2))) {
 235       size += (NativeMovConstReg::mov_n_three_orr_sz - NativeMovConstReg::movw_movt_pair_sz);
 236     }
 237   }
 238   return size;
 239 
 240   // In order to tune these parameters, run the JVM with VM options
 241   // +PrintMiscellaneous and +WizardMode to see information about
 242   // actual itable stubs.  Run it with -Xmx31G -XX:+UseCompressedOops.
 243   //
 244   // If Universe::narrow_klass_base is nonzero, decoding a compressed
 245   // class can take zeveral instructions.  Run it with -Xmx31G
 246   // -XX:+UseCompressedOops.
 247   //
 248   // The JVM98 app. _202_jess has a megamorphic interface call.
 249 }
 250 
 251 int VtableStub::pd_code_alignment() { return 4; }