duke@435: /* duke@435: * Copyright 2003-2006 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_64.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, duke@435: oop receiver, duke@435: int index); duke@435: #endif duke@435: duke@435: VtableStub* VtableStubs::create_vtable_stub(int vtable_index) { duke@435: const int amd64_code_length = VtableStub::pd_code_size_limit(true); duke@435: VtableStub* s = new(amd64_code_length) VtableStub(true, vtable_index); duke@435: ResourceMark rm; duke@435: CodeBuffer cb(s->entry_point(), amd64_code_length); duke@435: MacroAssembler* masm = new MacroAssembler(&cb); duke@435: duke@435: #ifndef PRODUCT duke@435: if (CountCompiledCalls) { duke@435: __ incrementl(ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr())); duke@435: } duke@435: #endif duke@435: duke@435: // get receiver (need to skip return address on top of stack) duke@435: assert(VtableStub::receiver_location() == j_rarg0->as_VMReg(), "receiver expected in j_rarg0"); duke@435: duke@435: // Free registers (non-args) are rax, rbx duke@435: duke@435: // get receiver klass duke@435: address npe_addr = __ pc(); duke@435: __ movq(rax, Address(j_rarg0, oopDesc::klass_offset_in_bytes())); duke@435: duke@435: // compute entry offset (in words) duke@435: int entry_offset = duke@435: instanceKlass::vtable_start_offset() + vtable_index * vtableEntry::size(); duke@435: 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), duke@435: vtable_index * vtableEntry::size()); duke@435: __ jcc(Assembler::greater, L); duke@435: __ movl(rbx, vtable_index); duke@435: __ call_VM(noreg, duke@435: CAST_FROM_FN_PTR(address, bad_compiled_vtable_index), j_rarg0, rbx); duke@435: __ bind(L); duke@435: } duke@435: #endif // PRODUCT duke@435: duke@435: // load methodOop and target address duke@435: const Register method = rbx; duke@435: duke@435: __ movq(method, Address(rax, duke@435: entry_offset * wordSize + duke@435: vtableEntry::method_offset_in_bytes())); duke@435: if (DebugVtables) { duke@435: Label L; duke@435: __ cmpq(method, (int)NULL); duke@435: __ jcc(Assembler::equal, L); duke@435: __ cmpq(Address(method, methodOopDesc::from_compiled_offset()), (int)NULL_WORD); duke@435: __ jcc(Assembler::notZero, L); duke@435: __ stop("Vtable entry is NULL"); duke@435: __ bind(L); duke@435: } duke@435: // rax: receiver klass duke@435: // rbx: methodOop duke@435: // rcx: receiver duke@435: address ame_addr = __ pc(); duke@435: __ jmp( Address(rbx, methodOopDesc::from_compiled_offset())); duke@435: duke@435: __ 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 duke@435: // away with. If you add code here, bump the code stub size duke@435: // returned by pd_code_size_limit! duke@435: const int amd64_code_length = VtableStub::pd_code_size_limit(false); duke@435: VtableStub* s = new(amd64_code_length) VtableStub(false, vtable_index); duke@435: ResourceMark rm; duke@435: CodeBuffer cb(s->entry_point(), amd64_code_length); duke@435: MacroAssembler* masm = new MacroAssembler(&cb); duke@435: duke@435: #ifndef PRODUCT duke@435: if (CountCompiledCalls) { duke@435: __ incrementl(ExternalAddress((address) SharedRuntime::nof_megamorphic_calls_addr())); duke@435: } duke@435: #endif duke@435: duke@435: // Entry arguments: duke@435: // rax: Interface duke@435: // j_rarg0: Receiver duke@435: duke@435: // Free registers (non-args) are rax (interface), rbx duke@435: duke@435: // get receiver (need to skip return address on top of stack) duke@435: duke@435: assert(VtableStub::receiver_location() == j_rarg0->as_VMReg(), "receiver expected in j_rarg0"); duke@435: // get receiver klass (also an implicit null-check) duke@435: address npe_addr = __ pc(); duke@435: duke@435: __ movq(rbx, Address(j_rarg0, oopDesc::klass_offset_in_bytes())); duke@435: duke@435: // If we take a trap while this arg is on the stack we will not duke@435: // be able to walk the stack properly. This is not an issue except duke@435: // when there are mistakes in this assembly code that could generate duke@435: // a spurious fault. Ask me how I know... duke@435: duke@435: __ pushq(j_rarg1); // Most registers are in use, so save one duke@435: duke@435: // compute itable entry offset (in words) duke@435: const int base = instanceKlass::vtable_start_offset() * wordSize; duke@435: assert(vtableEntry::size() * wordSize == 8, duke@435: "adjust the scaling in the code below"); duke@435: // Get length of vtable duke@435: __ movl(j_rarg1, duke@435: Address(rbx, instanceKlass::vtable_length_offset() * wordSize)); duke@435: __ leaq(rbx, Address(rbx, j_rarg1, Address::times_8, base)); duke@435: duke@435: if (HeapWordsPerLong > 1) { duke@435: // Round up to align_object_offset boundary duke@435: __ round_to_q(rbx, BytesPerLong); duke@435: } dcubed@451: Label hit, next, entry, throw_icce; duke@435: duke@435: __ jmpb(entry); duke@435: duke@435: __ bind(next); duke@435: __ addq(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 dcubed@451: __ movq(j_rarg1, Address(rbx, itableOffsetEntry::interface_offset_in_bytes())); dcubed@451: __ testq(j_rarg1, j_rarg1); dcubed@451: __ jcc(Assembler::zero, throw_icce); dcubed@451: __ cmpq(rax, j_rarg1); dcubed@451: __ jccb(Assembler::notEqual, next); duke@435: duke@435: // We found a hit, move offset into j_rarg1 duke@435: __ movl(j_rarg1, Address(rbx, itableOffsetEntry::offset_offset_in_bytes())); duke@435: duke@435: // Compute itableMethodEntry duke@435: const int method_offset = duke@435: (itableMethodEntry::size() * wordSize * vtable_index) + duke@435: itableMethodEntry::method_offset_in_bytes(); duke@435: duke@435: // Get methodOop and entrypoint for compiler duke@435: duke@435: // Get klass pointer again duke@435: __ movq(rax, Address(j_rarg0, oopDesc::klass_offset_in_bytes())); duke@435: duke@435: const Register method = rbx; duke@435: __ movq(method, Address(rax, j_rarg1, Address::times_1, method_offset)); duke@435: duke@435: // Restore saved register, before possible trap. duke@435: __ popq(j_rarg1); duke@435: duke@435: // method (rbx): methodOop duke@435: // j_rarg0: receiver duke@435: duke@435: duke@435: #ifdef ASSERT dcubed@451: if (DebugVtables) { dcubed@451: Label L2; dcubed@451: __ cmpq(method, (int)NULL); dcubed@451: __ jcc(Assembler::equal, L2); dcubed@451: __ cmpq(Address(method, methodOopDesc::from_compiled_offset()), (int)NULL_WORD); dcubed@451: __ jcc(Assembler::notZero, L2); dcubed@451: __ stop("compiler entrypoint is null"); dcubed@451: __ bind(L2); dcubed@451: } duke@435: #endif // ASSERT duke@435: dcubed@451: // rbx: methodOop dcubed@451: // j_rarg0: receiver dcubed@451: address ame_addr = __ pc(); dcubed@451: __ jmp(Address(method, methodOopDesc::from_compiled_offset())); dcubed@451: dcubed@451: __ bind(throw_icce); dcubed@451: // Restore saved register dcubed@451: __ popq(j_rarg1); dcubed@451: __ jump(RuntimeAddress(StubRoutines::throw_IncompatibleClassChangeError_entry())); duke@435: duke@435: __ 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: 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 ? 512 : 24) + (CountCompiledCalls ? 13 : 0); duke@435: } else { duke@435: // Itable stub size dcubed@451: return (DebugVtables ? 636 : 72) + (CountCompiledCalls ? 13 : 0); duke@435: } duke@435: } duke@435: duke@435: int VtableStub::pd_code_alignment() { duke@435: return wordSize; duke@435: }