aoqi@0: /* aoqi@0: * Copyright (c) 2004, 2012, 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.hpp" aoqi@0: #include "asm/codeBuffer.hpp" aoqi@0: #include "memory/metaspaceShared.hpp" aoqi@0: aoqi@0: // Generate the self-patching vtable method: aoqi@0: // aoqi@0: // This method will be called (as any other Klass virtual method) with aoqi@0: // the Klass itself as the first argument. Example: aoqi@0: // aoqi@0: // oop obj; aoqi@0: // int size = obj->klass()->oop_size(this); aoqi@0: // aoqi@0: // for which the virtual method call is Klass::oop_size(); aoqi@0: // aoqi@0: // The dummy method is called with the Klass object as the first aoqi@0: // operand, and an object as the second argument. aoqi@0: // aoqi@0: aoqi@0: //===================================================================== aoqi@0: aoqi@0: // All of the dummy methods in the vtable are essentially identical, aoqi@0: // differing only by an ordinal constant, and they bear no relationship aoqi@0: // to the original method which the caller intended. Also, there needs aoqi@0: // to be 'vtbl_list_size' instances of the vtable in order to aoqi@0: // differentiate between the 'vtable_list_size' original Klass objects. aoqi@0: aoqi@0: #define __ masm-> aoqi@0: aoqi@0: void MetaspaceShared::generate_vtable_methods(void** vtbl_list, aoqi@0: void** vtable, aoqi@0: char** md_top, aoqi@0: char* md_end, aoqi@0: char** mc_top, aoqi@0: char* mc_end) { aoqi@0: aoqi@0: intptr_t vtable_bytes = (num_virtuals * vtbl_list_size) * sizeof(void*); aoqi@0: *(intptr_t *)(*md_top) = vtable_bytes; aoqi@0: *md_top += sizeof(intptr_t); aoqi@0: void** dummy_vtable = (void**)*md_top; aoqi@0: *vtable = dummy_vtable; aoqi@0: *md_top += vtable_bytes; aoqi@0: aoqi@0: // Get ready to generate dummy methods. aoqi@0: aoqi@0: CodeBuffer cb((unsigned char*)*mc_top, mc_end - *mc_top); aoqi@0: MacroAssembler* masm = new MacroAssembler(&cb); aoqi@0: aoqi@0: Label common_code; aoqi@0: for (int i = 0; i < vtbl_list_size; ++i) { aoqi@0: for (int j = 0; j < num_virtuals; ++j) { aoqi@0: dummy_vtable[num_virtuals * i + j] = (void*)masm->pc(); aoqi@0: aoqi@0: // Load eax with a value indicating vtable/offset pair. aoqi@0: // -- bits[ 7..0] (8 bits) which virtual method in table? aoqi@0: // -- bits[12..8] (5 bits) which virtual method table? aoqi@0: // -- must fit in 13-bit instruction immediate field. aoqi@0: __ movl(rax, (i << 8) + j); aoqi@0: __ jmp(common_code); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: __ bind(common_code); aoqi@0: aoqi@0: // Expecting to be called with "thiscall" convections -- the arguments aoqi@0: // are on the stack and the "this" pointer is in c_rarg0. In addition, rax aoqi@0: // was set (above) to the offset of the method in the table. aoqi@0: aoqi@0: __ push(c_rarg1); // save & free register aoqi@0: __ push(c_rarg0); // save "this" aoqi@0: __ mov(c_rarg0, rax); aoqi@0: __ shrptr(c_rarg0, 8); // isolate vtable identifier. aoqi@0: __ shlptr(c_rarg0, LogBytesPerWord); aoqi@0: __ lea(c_rarg1, ExternalAddress((address)vtbl_list)); // ptr to correct vtable list. aoqi@0: __ addptr(c_rarg1, c_rarg0); // ptr to list entry. aoqi@0: __ movptr(c_rarg1, Address(c_rarg1, 0)); // get correct vtable address. aoqi@0: __ pop(c_rarg0); // restore "this" aoqi@0: __ movptr(Address(c_rarg0, 0), c_rarg1); // update vtable pointer. aoqi@0: aoqi@0: __ andptr(rax, 0x00ff); // isolate vtable method index aoqi@0: __ shlptr(rax, LogBytesPerWord); aoqi@0: __ addptr(rax, c_rarg1); // address of real method pointer. aoqi@0: __ pop(c_rarg1); // restore register. aoqi@0: __ movptr(rax, Address(rax, 0)); // get real method pointer. aoqi@0: __ jmp(rax); // jump to the real method. aoqi@0: aoqi@0: __ flush(); aoqi@0: aoqi@0: *mc_top = (char*)__ pc(); aoqi@0: }