duke@435: /* xdono@772: * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: #include "incls/_precompiled.incl" duke@435: #include "incls/_vtableStubs_x86_32.cpp.incl" duke@435: duke@435: // machine-dependent part of VtableStubs: create VtableStub of correct size and duke@435: // initialize its code duke@435: duke@435: #define __ masm-> duke@435: duke@435: #ifndef PRODUCT duke@435: extern "C" void bad_compiled_vtable_index(JavaThread* thread, oop receiver, int index); duke@435: #endif duke@435: duke@435: // used by compiler only; may use only caller saved registers rax, rbx, rcx. duke@435: // rdx holds first int arg, rsi, rdi, rbp are callee-save & must be preserved. duke@435: // Leave receiver in rcx; required behavior when +OptoArgsInRegisters duke@435: // is modifed to put first oop in rcx. duke@435: // duke@435: VtableStub* VtableStubs::create_vtable_stub(int vtable_index) { duke@435: const int i486_code_length = VtableStub::pd_code_size_limit(true); duke@435: VtableStub* s = new(i486_code_length) VtableStub(true, vtable_index); duke@435: ResourceMark rm; duke@435: CodeBuffer cb(s->entry_point(), i486_code_length); duke@435: MacroAssembler* masm = new MacroAssembler(&cb); duke@435: duke@435: #ifndef PRODUCT duke@435: duke@435: if (CountCompiledCalls) { never@739: __ incrementl(ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr())); duke@435: } duke@435: #endif /* PRODUCT */ duke@435: duke@435: // get receiver (need to skip return address on top of stack) duke@435: assert(VtableStub::receiver_location() == rcx->as_VMReg(), "receiver expected in rcx"); duke@435: duke@435: // get receiver klass duke@435: address npe_addr = __ pc(); never@739: __ movptr(rax, Address(rcx, oopDesc::klass_offset_in_bytes())); duke@435: // compute entry offset (in words) duke@435: int entry_offset = instanceKlass::vtable_start_offset() + vtable_index*vtableEntry::size(); duke@435: #ifndef PRODUCT duke@435: if (DebugVtables) { duke@435: Label L; duke@435: // check offset vs vtable length duke@435: __ cmpl(Address(rax, instanceKlass::vtable_length_offset()*wordSize), vtable_index*vtableEntry::size()); duke@435: __ jcc(Assembler::greater, L); duke@435: __ movl(rbx, vtable_index); duke@435: __ call_VM(noreg, CAST_FROM_FN_PTR(address, bad_compiled_vtable_index), rcx, rbx); duke@435: __ bind(L); duke@435: } duke@435: #endif // PRODUCT duke@435: duke@435: const Register method = rbx; duke@435: duke@435: // load methodOop and target address never@739: __ movptr(method, Address(rax, entry_offset*wordSize + vtableEntry::method_offset_in_bytes())); duke@435: if (DebugVtables) { duke@435: Label L; never@739: __ cmpptr(method, (int32_t)NULL_WORD); duke@435: __ jcc(Assembler::equal, L); never@739: __ cmpptr(Address(method, methodOopDesc::from_compiled_offset()), (int32_t)NULL_WORD); duke@435: __ jcc(Assembler::notZero, L); duke@435: __ stop("Vtable entry is NULL"); duke@435: __ bind(L); duke@435: } duke@435: duke@435: // rax,: receiver klass duke@435: // method (rbx): methodOop duke@435: // rcx: receiver duke@435: address ame_addr = __ pc(); duke@435: __ jmp( Address(method, methodOopDesc::from_compiled_offset())); duke@435: duke@435: masm->flush(); duke@435: s->set_exception_points(npe_addr, ame_addr); duke@435: return s; duke@435: } duke@435: duke@435: duke@435: VtableStub* VtableStubs::create_itable_stub(int vtable_index) { duke@435: // Note well: pd_code_size_limit is the absolute minimum we can get away with. If you duke@435: // add code here, bump the code stub size returned by pd_code_size_limit! duke@435: const int i486_code_length = VtableStub::pd_code_size_limit(false); duke@435: VtableStub* s = new(i486_code_length) VtableStub(false, vtable_index); duke@435: ResourceMark rm; duke@435: CodeBuffer cb(s->entry_point(), i486_code_length); duke@435: MacroAssembler* masm = new MacroAssembler(&cb); duke@435: duke@435: // Entry arguments: duke@435: // rax,: Interface duke@435: // rcx: Receiver duke@435: duke@435: #ifndef PRODUCT duke@435: if (CountCompiledCalls) { never@739: __ incrementl(ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr())); duke@435: } duke@435: #endif /* PRODUCT */ duke@435: // get receiver (need to skip return address on top of stack) duke@435: duke@435: assert(VtableStub::receiver_location() == rcx->as_VMReg(), "receiver expected in rcx"); duke@435: duke@435: // get receiver klass (also an implicit null-check) duke@435: address npe_addr = __ pc(); never@739: __ movptr(rbx, Address(rcx, oopDesc::klass_offset_in_bytes())); duke@435: never@739: __ mov(rsi, rbx); // Save klass in free register duke@435: // Most registers are in use, so save a few never@739: __ push(rdx); duke@435: // compute itable entry offset (in words) duke@435: const int base = instanceKlass::vtable_start_offset() * wordSize; duke@435: assert(vtableEntry::size() * wordSize == 4, "adjust the scaling in the code below"); duke@435: __ movl(rdx, Address(rbx, instanceKlass::vtable_length_offset() * wordSize)); // Get length of vtable never@739: __ lea(rbx, Address(rbx, rdx, Address::times_ptr, base)); duke@435: if (HeapWordsPerLong > 1) { duke@435: // Round up to align_object_offset boundary duke@435: __ round_to(rbx, BytesPerLong); duke@435: } duke@435: dcubed@451: Label hit, next, entry, throw_icce; duke@435: dcubed@451: __ jmpb(entry); duke@435: duke@435: __ bind(next); never@739: __ addptr(rbx, itableOffsetEntry::size() * wordSize); duke@435: duke@435: __ bind(entry); duke@435: dcubed@451: // If the entry is NULL then we've reached the end of the table dcubed@451: // without finding the expected interface, so throw an exception never@739: __ movptr(rdx, Address(rbx, itableOffsetEntry::interface_offset_in_bytes())); never@739: __ testptr(rdx, rdx); dcubed@451: __ jcc(Assembler::zero, throw_icce); never@739: __ cmpptr(rax, rdx); duke@435: __ jcc(Assembler::notEqual, next); duke@435: duke@435: // We found a hit, move offset into rbx, duke@435: __ movl(rdx, Address(rbx, itableOffsetEntry::offset_offset_in_bytes())); duke@435: duke@435: // Compute itableMethodEntry. duke@435: const int method_offset = (itableMethodEntry::size() * wordSize * vtable_index) + itableMethodEntry::method_offset_in_bytes(); duke@435: duke@435: // Get methodOop and entrypoint for compiler duke@435: const Register method = rbx; never@739: __ movptr(method, Address(rsi, rdx, Address::times_1, method_offset)); duke@435: duke@435: // Restore saved register, before possible trap. never@739: __ pop(rdx); duke@435: duke@435: // method (rbx): methodOop duke@435: // rcx: receiver duke@435: duke@435: #ifdef ASSERT duke@435: if (DebugVtables) { duke@435: Label L1; never@739: __ cmpptr(method, (int32_t)NULL_WORD); duke@435: __ jcc(Assembler::equal, L1); never@739: __ cmpptr(Address(method, methodOopDesc::from_compiled_offset()), (int32_t)NULL_WORD); duke@435: __ jcc(Assembler::notZero, L1); duke@435: __ stop("methodOop is null"); duke@435: __ bind(L1); duke@435: } duke@435: #endif // ASSERT duke@435: duke@435: address ame_addr = __ pc(); duke@435: __ jmp(Address(method, methodOopDesc::from_compiled_offset())); duke@435: dcubed@451: __ bind(throw_icce); dcubed@451: // Restore saved register never@739: __ pop(rdx); dcubed@451: __ jump(RuntimeAddress(StubRoutines::throw_IncompatibleClassChangeError_entry())); dcubed@451: duke@435: masm->flush(); dcubed@451: dcubed@451: guarantee(__ pc() <= s->code_end(), "overflowed buffer"); dcubed@451: duke@435: s->set_exception_points(npe_addr, ame_addr); duke@435: return s; duke@435: } duke@435: duke@435: duke@435: duke@435: int VtableStub::pd_code_size_limit(bool is_vtable_stub) { duke@435: if (is_vtable_stub) { duke@435: // Vtable stub size duke@435: return (DebugVtables ? 210 : 16) + (CountCompiledCalls ? 6 : 0); duke@435: } else { duke@435: // Itable stub size dcubed@451: return (DebugVtables ? 144 : 64) + (CountCompiledCalls ? 6 : 0); duke@435: } duke@435: } duke@435: duke@435: int VtableStub::pd_code_alignment() { duke@435: return wordSize; duke@435: }