vtableStubs_sparc.cpp 10.1 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25
#include "precompiled.hpp"
26
#include "asm/macroAssembler.inline.hpp"
27 28 29 30 31 32 33 34 35 36
#include "code/vtableStubs.hpp"
#include "interp_masm_sparc.hpp"
#include "memory/resourceArea.hpp"
#include "oops/instanceKlass.hpp"
#include "oops/klassVtable.hpp"
#include "runtime/sharedRuntime.hpp"
#include "vmreg_sparc.inline.hpp"
#ifdef COMPILER2
#include "opto/runtime.hpp"
#endif
D
duke 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

// machine-dependent part of VtableStubs: create vtableStub of correct size and
// initialize its code

#define __ masm->


#ifndef PRODUCT
extern "C" void bad_compiled_vtable_index(JavaThread* thread, oopDesc* receiver, int index);
#endif


// Used by compiler only; may use only caller saved, non-argument registers
// NOTE:  %%%% if any change is made to this stub make sure that the function
//             pd_code_size_limit is changed to ensure the correct size for VtableStub
VtableStub* VtableStubs::create_vtable_stub(int vtable_index) {
  const int sparc_code_length = VtableStub::pd_code_size_limit(true);
  VtableStub* s = new(sparc_code_length) VtableStub(true, vtable_index);
55 56 57 58 59
  // Can be NULL if there is no free space in the code cache.
  if (s == NULL) {
    return NULL;
  }

D
duke 已提交
60 61 62 63 64 65
  ResourceMark rm;
  CodeBuffer cb(s->entry_point(), sparc_code_length);
  MacroAssembler* masm = new MacroAssembler(&cb);

#ifndef PRODUCT
  if (CountCompiledCalls) {
66
    __ inc_counter(SharedRuntime::nof_megamorphic_calls_addr(), G5, G3_scratch);
D
duke 已提交
67 68 69 70 71 72 73
  }
#endif /* PRODUCT */

  assert(VtableStub::receiver_location() == O0->as_VMReg(), "receiver expected in O0");

  // get receiver klass
  address npe_addr = __ pc();
74
  __ load_klass(O0, G3_scratch);
D
duke 已提交
75

76
  // set Method* (in case of interpreted method), and destination address
D
duke 已提交
77 78 79 80
#ifndef PRODUCT
  if (DebugVtables) {
    Label L;
    // check offset vs vtable length
81
    __ ld(G3_scratch, InstanceKlass::vtable_length_offset()*wordSize, G5);
K
kvn 已提交
82
    __ cmp_and_br_short(G5, vtable_index*vtableEntry::size(), Assembler::greaterUnsigned, Assembler::pt, L);
D
duke 已提交
83 84 85 86 87
    __ set(vtable_index, O2);
    __ call_VM(noreg, CAST_FROM_FN_PTR(address, bad_compiled_vtable_index), O0, O2);
    __ bind(L);
  }
#endif
88 89

  __ lookup_virtual_method(G3_scratch, vtable_index, G5_method);
D
duke 已提交
90 91 92 93

#ifndef PRODUCT
  if (DebugVtables) {
    Label L;
K
kvn 已提交
94
    __ br_notnull_short(G5_method, Assembler::pt, L);
D
duke 已提交
95 96 97 98 99 100 101 102
    __ stop("Vtable entry is ZERO");
    __ bind(L);
  }
#endif

  address ame_addr = __ pc();  // if the vtable entry is null, the method is abstract
                               // NOTE: for vtable dispatches, the vtable entry will never be null.

103
  __ ld_ptr(G5_method, in_bytes(Method::from_compiled_offset()), G3_scratch);
D
duke 已提交
104 105 106

  // jump to target (either compiled code or c2iadapter)
  __ JMP(G3_scratch, 0);
107
  // load Method* (in case we call c2iadapter)
D
duke 已提交
108 109 110
  __ delayed()->nop();

  masm->flush();
111 112 113 114 115 116 117 118

  if (PrintMiscellaneous && (WizardMode || Verbose)) {
    tty->print_cr("vtable #%d at "PTR_FORMAT"[%d] left over: %d",
                  vtable_index, s->entry_point(),
                  (int)(s->code_end() - s->entry_point()),
                  (int)(s->code_end() - __ pc()));
  }
  guarantee(__ pc() <= s->code_end(), "overflowed buffer");
119 120 121
  // shut the door on sizing bugs
  int slop = 2*BytesPerInstWord;  // 32-bit offset is this much larger than a 13-bit one
  assert(vtable_index > 10 || __ pc() + slop <= s->code_end(), "room for sethi;add");
122

D
duke 已提交
123 124 125 126 127 128 129
  s->set_exception_points(npe_addr, ame_addr);
  return s;
}


// NOTE:  %%%% if any change is made to this stub make sure that the function
//             pd_code_size_limit is changed to ensure the correct size for VtableStub
130
VtableStub* VtableStubs::create_itable_stub(int itable_index) {
D
duke 已提交
131
  const int sparc_code_length = VtableStub::pd_code_size_limit(false);
132
  VtableStub* s = new(sparc_code_length) VtableStub(false, itable_index);
133 134 135 136 137
  // Can be NULL if there is no free space in the code cache.
  if (s == NULL) {
    return NULL;
  }

D
duke 已提交
138 139 140 141
  ResourceMark rm;
  CodeBuffer cb(s->entry_point(), sparc_code_length);
  MacroAssembler* masm = new MacroAssembler(&cb);

142
  Register G3_Klass = G3_scratch;
D
duke 已提交
143 144 145 146 147 148 149 150 151 152
  Register G5_interface = G5;  // Passed in as an argument
  Label search;

  // Entry arguments:
  //  G5_interface: Interface
  //  O0:           Receiver
  assert(VtableStub::receiver_location() == O0->as_VMReg(), "receiver expected in O0");

  // get receiver klass (also an implicit null-check)
  address npe_addr = __ pc();
153
  __ load_klass(O0, G3_Klass);
D
duke 已提交
154 155 156 157 158 159 160 161 162

  // Push a new window to get some temp registers.  This chops the head of all
  // my 64-bit %o registers in the LION build, but this is OK because no longs
  // are passed in the %o registers.  Instead, longs are passed in G1 and G4
  // and so those registers are not available here.
  __ save(SP,-frame::register_save_words*wordSize,SP);

#ifndef PRODUCT
  if (CountCompiledCalls) {
163
    __ inc_counter(SharedRuntime::nof_megamorphic_calls_addr(), L0, L1);
D
duke 已提交
164 165 166
  }
#endif /* PRODUCT */

167
  Label throw_icce;
D
duke 已提交
168

169 170
  Register L5_method = L5;
  __ lookup_interface_method(// inputs: rec. class, interface, itable index
171
                             G3_Klass, G5_interface, itable_index,
172 173 174
                             // outputs: method, scan temp. reg
                             L5_method, L2, L3,
                             throw_icce);
D
duke 已提交
175 176 177 178

#ifndef PRODUCT
  if (DebugVtables) {
    Label L01;
K
kvn 已提交
179
    __ br_notnull_short(L5_method, Assembler::pt, L01);
180
    __ stop("Method* is null");
D
duke 已提交
181 182 183 184 185 186 187
    __ bind(L01);
  }
#endif

  // If the following load is through a NULL pointer, we'll take an OS
  // exception that should translate into an AbstractMethodError.  We need the
  // window count to be correct at that time.
188 189
  __ restore(L5_method, 0, G5_method);
  // Restore registers *before* the AME point.
D
duke 已提交
190 191

  address ame_addr = __ pc();   // if the vtable entry is null, the method is abstract
192
  __ ld_ptr(G5_method, in_bytes(Method::from_compiled_offset()), G3_scratch);
D
duke 已提交
193

194
  // G5_method:  Method*
D
duke 已提交
195 196 197 198 199
  // O0:         Receiver
  // G3_scratch: entry point
  __ JMP(G3_scratch, 0);
  __ delayed()->nop();

200
  __ bind(throw_icce);
201 202
  AddressLiteral icce(StubRoutines::throw_IncompatibleClassChangeError_entry());
  __ jump_to(icce, G3_scratch);
203 204
  __ delayed()->restore();

D
duke 已提交
205
  masm->flush();
206

207 208 209 210 211 212
  if (PrintMiscellaneous && (WizardMode || Verbose)) {
    tty->print_cr("itable #%d at "PTR_FORMAT"[%d] left over: %d",
                  itable_index, s->entry_point(),
                  (int)(s->code_end() - s->entry_point()),
                  (int)(s->code_end() - __ pc()));
  }
213
  guarantee(__ pc() <= s->code_end(), "overflowed buffer");
214 215 216
  // shut the door on sizing bugs
  int slop = 2*BytesPerInstWord;  // 32-bit offset is this much larger than a 13-bit one
  assert(itable_index > 10 || __ pc() + slop <= s->code_end(), "room for sethi;add");
217

D
duke 已提交
218 219 220 221 222 223
  s->set_exception_points(npe_addr, ame_addr);
  return s;
}


int VtableStub::pd_code_size_limit(bool is_vtable_stub) {
224
  if (TraceJumps || DebugVtables || CountCompiledCalls || VerifyOops) return 1000;
D
duke 已提交
225 226 227
  else {
    const int slop = 2*BytesPerInstWord; // sethi;add  (needed for long offsets)
    if (is_vtable_stub) {
228 229
      // ld;ld;ld,jmp,nop
      const int basic = 5*BytesPerInstWord +
230
                        // shift;add for load_klass (only shift with zero heap based)
231
                        (UseCompressedClassPointers ?
232
                          MacroAssembler::instr_size_for_decode_klass_not_null() : 0);
D
duke 已提交
233 234
      return basic + slop;
    } else {
235
      const int basic = (28 LP64_ONLY(+ 6)) * BytesPerInstWord +
236
                        // shift;add for load_klass (only shift with zero heap based)
237
                        (UseCompressedClassPointers ?
238
                          MacroAssembler::instr_size_for_decode_klass_not_null() : 0);
D
duke 已提交
239 240 241
      return (basic + slop);
    }
  }
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

  // In order to tune these parameters, run the JVM with VM options
  // +PrintMiscellaneous and +WizardMode to see information about
  // actual itable stubs.  Look for lines like this:
  //   itable #1 at 0x5551212[116] left over: 8
  // Reduce the constants so that the "left over" number is 8
  // Do not aim at a left-over number of zero, because a very
  // large vtable or itable offset (> 4K) will require an extra
  // sethi/or pair of instructions.
  //
  // The JVM98 app. _202_jess has a megamorphic interface call.
  // The itable code looks like this:
  // Decoding VtableStub itbl[1]@16
  //   ld  [ %o0 + 4 ], %g3
  //   save  %sp, -64, %sp
  //   ld  [ %g3 + 0xe8 ], %l2
  //   sll  %l2, 2, %l2
  //   add  %l2, 0x134, %l2
  //   and  %l2, -8, %l2        ! NOT_LP64 only
  //   add  %g3, %l2, %l2
  //   add  %g3, 4, %g3
  //   ld  [ %l2 ], %l5
  //   brz,pn   %l5, throw_icce
  //   cmp  %l5, %g5
  //   be  %icc, success
  //   add  %l2, 8, %l2
  // loop:
  //   ld  [ %l2 ], %l5
  //   brz,pn   %l5, throw_icce
  //   cmp  %l5, %g5
  //   bne,pn   %icc, loop
  //   add  %l2, 8, %l2
  // success:
  //   ld  [ %l2 + -4 ], %l2
  //   ld  [ %g3 + %l2 ], %l5
  //   restore  %l5, 0, %g5
  //   ld  [ %g5 + 0x44 ], %g3
  //   jmp  %g3
  //   nop
  // throw_icce:
  //   sethi  %hi(throw_ICCE_entry), %g3
  //   ! 5 more instructions here, LP64_ONLY
  //   jmp  %g3 + %lo(throw_ICCE_entry)
  //   restore
D
duke 已提交
286 287 288 289 290 291 292 293
}


int VtableStub::pd_code_alignment() {
  // UltraSPARC cache line size is 8 instructions:
  const unsigned int icache_line_size = 32;
  return icache_line_size;
}