0N/A/*
1879N/A * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "assembler_x86.inline.hpp"
1879N/A#include "memory/compactingPermGenGen.hpp"
1879N/A#include "memory/generation.inline.hpp"
1879N/A#include "memory/space.inline.hpp"
0N/A
0N/A
0N/A
0N/A// Generate the self-patching vtable method:
0N/A//
0N/A// This method will be called (as any other Klass virtual method) with
0N/A// the Klass itself as the first argument. Example:
0N/A//
0N/A// oop obj;
0N/A// int size = obj->klass()->klass_part()->oop_size(this);
0N/A//
0N/A// for which the virtual method call is Klass::oop_size();
0N/A//
0N/A// The dummy method is called with the Klass object as the first
0N/A// operand, and an object as the second argument.
0N/A//
0N/A
0N/A//=====================================================================
0N/A
0N/A// All of the dummy methods in the vtable are essentially identical,
0N/A// differing only by an ordinal constant, and they bear no releationship
0N/A// to the original method which the caller intended. Also, there needs
0N/A// to be 'vtbl_list_size' instances of the vtable in order to
0N/A// differentiate between the 'vtable_list_size' original Klass objects.
0N/A
0N/A#define __ masm->
0N/A
0N/Avoid CompactingPermGenGen::generate_vtable_methods(void** vtbl_list,
0N/A void** vtable,
0N/A char** md_top,
0N/A char* md_end,
0N/A char** mc_top,
0N/A char* mc_end) {
0N/A
0N/A intptr_t vtable_bytes = (num_virtuals * vtbl_list_size) * sizeof(void*);
0N/A *(intptr_t *)(*md_top) = vtable_bytes;
0N/A *md_top += sizeof(intptr_t);
0N/A void** dummy_vtable = (void**)*md_top;
0N/A *vtable = dummy_vtable;
0N/A *md_top += vtable_bytes;
0N/A
0N/A // Get ready to generate dummy methods.
0N/A
0N/A CodeBuffer cb((unsigned char*)*mc_top, mc_end - *mc_top);
0N/A MacroAssembler* masm = new MacroAssembler(&cb);
0N/A
0N/A Label common_code;
0N/A for (int i = 0; i < vtbl_list_size; ++i) {
0N/A for (int j = 0; j < num_virtuals; ++j) {
0N/A dummy_vtable[num_virtuals * i + j] = (void*)masm->pc();
0N/A
0N/A // Load rax, with a value indicating vtable/offset pair.
0N/A // -- bits[ 7..0] (8 bits) which virtual method in table?
0N/A // -- bits[12..8] (5 bits) which virtual method table?
0N/A // -- must fit in 13-bit instruction immediate field.
0N/A __ movl(rax, (i << 8) + j);
0N/A __ jmp(common_code);
0N/A }
0N/A }
0N/A
0N/A __ bind(common_code);
0N/A
0N/A#ifdef WIN32
0N/A // Expecting to be called with "thiscall" conventions -- the arguments
0N/A // are on the stack, except that the "this" pointer is in rcx.
0N/A#else
0N/A // Expecting to be called with Unix conventions -- the arguments
0N/A // are on the stack, including the "this" pointer.
0N/A#endif
0N/A
0N/A // In addition, rax was set (above) to the offset of the method in the
0N/A // table.
0N/A
0N/A#ifdef WIN32
304N/A __ push(rcx); // save "this"
0N/A#endif
304N/A __ mov(rcx, rax);
304N/A __ shrptr(rcx, 8); // isolate vtable identifier.
304N/A __ shlptr(rcx, LogBytesPerWord);
0N/A Address index(noreg, rcx, Address::times_1);
0N/A ExternalAddress vtbl((address)vtbl_list);
0N/A __ movptr(rdx, ArrayAddress(vtbl, index)); // get correct vtable address.
0N/A#ifdef WIN32
304N/A __ pop(rcx); // restore "this"
0N/A#else
304N/A __ movptr(rcx, Address(rsp, BytesPerWord)); // fetch "this"
0N/A#endif
304N/A __ movptr(Address(rcx, 0), rdx); // update vtable pointer.
0N/A
304N/A __ andptr(rax, 0x00ff); // isolate vtable method index
304N/A __ shlptr(rax, LogBytesPerWord);
304N/A __ addptr(rax, rdx); // address of real method pointer.
0N/A __ jmp(Address(rax, 0)); // get real method pointer.
0N/A
0N/A __ flush();
0N/A
0N/A *mc_top = (char*)__ pc();
0N/A}