aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "asm/macroAssembler.inline.hpp" aoqi@0: #include "code/vtableStubs.hpp" aoqi@0: #include "interp_masm_sparc.hpp" aoqi@0: #include "memory/resourceArea.hpp" aoqi@0: #include "oops/instanceKlass.hpp" aoqi@0: #include "oops/klassVtable.hpp" aoqi@0: #include "runtime/sharedRuntime.hpp" aoqi@0: #include "vmreg_sparc.inline.hpp" aoqi@0: #ifdef COMPILER2 aoqi@0: #include "opto/runtime.hpp" aoqi@0: #endif aoqi@0: aoqi@0: // machine-dependent part of VtableStubs: create vtableStub of correct size and aoqi@0: // initialize its code aoqi@0: aoqi@0: #define __ masm-> aoqi@0: aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: extern "C" void bad_compiled_vtable_index(JavaThread* thread, oopDesc* receiver, int index); aoqi@0: #endif aoqi@0: aoqi@0: aoqi@0: // Used by compiler only; may use only caller saved, non-argument registers aoqi@0: // NOTE: %%%% if any change is made to this stub make sure that the function aoqi@0: // pd_code_size_limit is changed to ensure the correct size for VtableStub aoqi@0: VtableStub* VtableStubs::create_vtable_stub(int vtable_index) { aoqi@0: const int sparc_code_length = VtableStub::pd_code_size_limit(true); aoqi@0: VtableStub* s = new(sparc_code_length) VtableStub(true, vtable_index); aoqi@0: // Can be NULL if there is no free space in the code cache. aoqi@0: if (s == NULL) { aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: ResourceMark rm; aoqi@0: CodeBuffer cb(s->entry_point(), sparc_code_length); aoqi@0: MacroAssembler* masm = new MacroAssembler(&cb); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: if (CountCompiledCalls) { aoqi@0: __ inc_counter(SharedRuntime::nof_megamorphic_calls_addr(), G5, G3_scratch); aoqi@0: } aoqi@0: #endif /* PRODUCT */ aoqi@0: aoqi@0: assert(VtableStub::receiver_location() == O0->as_VMReg(), "receiver expected in O0"); aoqi@0: aoqi@0: // get receiver klass aoqi@0: address npe_addr = __ pc(); aoqi@0: __ load_klass(O0, G3_scratch); aoqi@0: aoqi@0: // set Method* (in case of interpreted method), and destination address aoqi@0: #ifndef PRODUCT aoqi@0: if (DebugVtables) { aoqi@0: Label L; aoqi@0: // check offset vs vtable length aoqi@0: __ ld(G3_scratch, InstanceKlass::vtable_length_offset()*wordSize, G5); aoqi@0: __ cmp_and_br_short(G5, vtable_index*vtableEntry::size(), Assembler::greaterUnsigned, Assembler::pt, L); aoqi@0: __ set(vtable_index, O2); aoqi@0: __ call_VM(noreg, CAST_FROM_FN_PTR(address, bad_compiled_vtable_index), O0, O2); aoqi@0: __ bind(L); aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: __ lookup_virtual_method(G3_scratch, vtable_index, G5_method); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: if (DebugVtables) { aoqi@0: Label L; aoqi@0: __ br_notnull_short(G5_method, Assembler::pt, L); aoqi@0: __ stop("Vtable entry is ZERO"); aoqi@0: __ bind(L); aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: address ame_addr = __ pc(); // if the vtable entry is null, the method is abstract aoqi@0: // NOTE: for vtable dispatches, the vtable entry will never be null. aoqi@0: aoqi@0: __ ld_ptr(G5_method, in_bytes(Method::from_compiled_offset()), G3_scratch); aoqi@0: aoqi@0: // jump to target (either compiled code or c2iadapter) aoqi@0: __ JMP(G3_scratch, 0); aoqi@0: // load Method* (in case we call c2iadapter) aoqi@0: __ delayed()->nop(); aoqi@0: aoqi@0: masm->flush(); aoqi@0: aoqi@0: if (PrintMiscellaneous && (WizardMode || Verbose)) { aoqi@0: tty->print_cr("vtable #%d at "PTR_FORMAT"[%d] left over: %d", aoqi@0: vtable_index, s->entry_point(), aoqi@0: (int)(s->code_end() - s->entry_point()), aoqi@0: (int)(s->code_end() - __ pc())); aoqi@0: } aoqi@0: guarantee(__ pc() <= s->code_end(), "overflowed buffer"); aoqi@0: // shut the door on sizing bugs aoqi@0: int slop = 2*BytesPerInstWord; // 32-bit offset is this much larger than a 13-bit one aoqi@0: assert(vtable_index > 10 || __ pc() + slop <= s->code_end(), "room for sethi;add"); aoqi@0: aoqi@0: s->set_exception_points(npe_addr, ame_addr); aoqi@0: return s; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // NOTE: %%%% if any change is made to this stub make sure that the function aoqi@0: // pd_code_size_limit is changed to ensure the correct size for VtableStub aoqi@0: VtableStub* VtableStubs::create_itable_stub(int itable_index) { aoqi@0: const int sparc_code_length = VtableStub::pd_code_size_limit(false); aoqi@0: VtableStub* s = new(sparc_code_length) VtableStub(false, itable_index); aoqi@0: // Can be NULL if there is no free space in the code cache. aoqi@0: if (s == NULL) { aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: ResourceMark rm; aoqi@0: CodeBuffer cb(s->entry_point(), sparc_code_length); aoqi@0: MacroAssembler* masm = new MacroAssembler(&cb); aoqi@0: aoqi@0: Register G3_Klass = G3_scratch; aoqi@0: Register G5_interface = G5; // Passed in as an argument aoqi@0: Label search; aoqi@0: aoqi@0: // Entry arguments: aoqi@0: // G5_interface: Interface aoqi@0: // O0: Receiver aoqi@0: assert(VtableStub::receiver_location() == O0->as_VMReg(), "receiver expected in O0"); aoqi@0: aoqi@0: // get receiver klass (also an implicit null-check) aoqi@0: address npe_addr = __ pc(); aoqi@0: __ load_klass(O0, G3_Klass); aoqi@0: aoqi@0: // Push a new window to get some temp registers. This chops the head of all aoqi@0: // my 64-bit %o registers in the LION build, but this is OK because no longs aoqi@0: // are passed in the %o registers. Instead, longs are passed in G1 and G4 aoqi@0: // and so those registers are not available here. aoqi@0: __ save(SP,-frame::register_save_words*wordSize,SP); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: if (CountCompiledCalls) { aoqi@0: __ inc_counter(SharedRuntime::nof_megamorphic_calls_addr(), L0, L1); aoqi@0: } aoqi@0: #endif /* PRODUCT */ aoqi@0: aoqi@0: Label throw_icce; aoqi@0: aoqi@0: Register L5_method = L5; aoqi@0: __ lookup_interface_method(// inputs: rec. class, interface, itable index aoqi@0: G3_Klass, G5_interface, itable_index, aoqi@0: // outputs: method, scan temp. reg aoqi@0: L5_method, L2, L3, aoqi@0: throw_icce); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: if (DebugVtables) { aoqi@0: Label L01; aoqi@0: __ br_notnull_short(L5_method, Assembler::pt, L01); aoqi@0: __ stop("Method* is null"); aoqi@0: __ bind(L01); aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: // If the following load is through a NULL pointer, we'll take an OS aoqi@0: // exception that should translate into an AbstractMethodError. We need the aoqi@0: // window count to be correct at that time. aoqi@0: __ restore(L5_method, 0, G5_method); aoqi@0: // Restore registers *before* the AME point. aoqi@0: aoqi@0: address ame_addr = __ pc(); // if the vtable entry is null, the method is abstract aoqi@0: __ ld_ptr(G5_method, in_bytes(Method::from_compiled_offset()), G3_scratch); aoqi@0: aoqi@0: // G5_method: Method* aoqi@0: // O0: Receiver aoqi@0: // G3_scratch: entry point aoqi@0: __ JMP(G3_scratch, 0); aoqi@0: __ delayed()->nop(); aoqi@0: aoqi@0: __ bind(throw_icce); aoqi@0: AddressLiteral icce(StubRoutines::throw_IncompatibleClassChangeError_entry()); aoqi@0: __ jump_to(icce, G3_scratch); aoqi@0: __ delayed()->restore(); aoqi@0: aoqi@0: masm->flush(); aoqi@0: aoqi@0: if (PrintMiscellaneous && (WizardMode || Verbose)) { aoqi@0: tty->print_cr("itable #%d at "PTR_FORMAT"[%d] left over: %d", aoqi@0: itable_index, s->entry_point(), aoqi@0: (int)(s->code_end() - s->entry_point()), aoqi@0: (int)(s->code_end() - __ pc())); aoqi@0: } aoqi@0: guarantee(__ pc() <= s->code_end(), "overflowed buffer"); aoqi@0: // shut the door on sizing bugs aoqi@0: int slop = 2*BytesPerInstWord; // 32-bit offset is this much larger than a 13-bit one aoqi@0: assert(itable_index > 10 || __ pc() + slop <= s->code_end(), "room for sethi;add"); aoqi@0: aoqi@0: s->set_exception_points(npe_addr, ame_addr); aoqi@0: return s; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: int VtableStub::pd_code_size_limit(bool is_vtable_stub) { aoqi@0: if (TraceJumps || DebugVtables || CountCompiledCalls || VerifyOops) return 1000; aoqi@0: else { aoqi@0: const int slop = 2*BytesPerInstWord; // sethi;add (needed for long offsets) aoqi@0: if (is_vtable_stub) { aoqi@0: // ld;ld;ld,jmp,nop aoqi@0: const int basic = 5*BytesPerInstWord + aoqi@0: // shift;add for load_klass (only shift with zero heap based) aoqi@0: (UseCompressedClassPointers ? aoqi@0: MacroAssembler::instr_size_for_decode_klass_not_null() : 0); aoqi@0: return basic + slop; aoqi@0: } else { aoqi@0: const int basic = (28 LP64_ONLY(+ 6)) * BytesPerInstWord + aoqi@0: // shift;add for load_klass (only shift with zero heap based) aoqi@0: (UseCompressedClassPointers ? aoqi@0: MacroAssembler::instr_size_for_decode_klass_not_null() : 0); aoqi@0: return (basic + slop); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // In order to tune these parameters, run the JVM with VM options aoqi@0: // +PrintMiscellaneous and +WizardMode to see information about aoqi@0: // actual itable stubs. Look for lines like this: aoqi@0: // itable #1 at 0x5551212[116] left over: 8 aoqi@0: // Reduce the constants so that the "left over" number is 8 aoqi@0: // Do not aim at a left-over number of zero, because a very aoqi@0: // large vtable or itable offset (> 4K) will require an extra aoqi@0: // sethi/or pair of instructions. aoqi@0: // aoqi@0: // The JVM98 app. _202_jess has a megamorphic interface call. aoqi@0: // The itable code looks like this: aoqi@0: // Decoding VtableStub itbl[1]@16 aoqi@0: // ld [ %o0 + 4 ], %g3 aoqi@0: // save %sp, -64, %sp aoqi@0: // ld [ %g3 + 0xe8 ], %l2 aoqi@0: // sll %l2, 2, %l2 aoqi@0: // add %l2, 0x134, %l2 aoqi@0: // and %l2, -8, %l2 ! NOT_LP64 only aoqi@0: // add %g3, %l2, %l2 aoqi@0: // add %g3, 4, %g3 aoqi@0: // ld [ %l2 ], %l5 aoqi@0: // brz,pn %l5, throw_icce aoqi@0: // cmp %l5, %g5 aoqi@0: // be %icc, success aoqi@0: // add %l2, 8, %l2 aoqi@0: // loop: aoqi@0: // ld [ %l2 ], %l5 aoqi@0: // brz,pn %l5, throw_icce aoqi@0: // cmp %l5, %g5 aoqi@0: // bne,pn %icc, loop aoqi@0: // add %l2, 8, %l2 aoqi@0: // success: aoqi@0: // ld [ %l2 + -4 ], %l2 aoqi@0: // ld [ %g3 + %l2 ], %l5 aoqi@0: // restore %l5, 0, %g5 aoqi@0: // ld [ %g5 + 0x44 ], %g3 aoqi@0: // jmp %g3 aoqi@0: // nop aoqi@0: // throw_icce: aoqi@0: // sethi %hi(throw_ICCE_entry), %g3 aoqi@0: // ! 5 more instructions here, LP64_ONLY aoqi@0: // jmp %g3 + %lo(throw_ICCE_entry) aoqi@0: // restore aoqi@0: } aoqi@0: aoqi@0: aoqi@0: int VtableStub::pd_code_alignment() { aoqi@0: // UltraSPARC cache line size is 8 instructions: aoqi@0: const unsigned int icache_line_size = 32; aoqi@0: return icache_line_size; aoqi@0: }