aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2014, 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/macroAssembler.inline.hpp" aoqi@0: #include "code/codeCache.hpp" aoqi@0: #include "compiler/disassembler.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "prims/forte.hpp" aoqi@0: #include "runtime/stubCodeGenerator.hpp" aoqi@0: aoqi@0: aoqi@0: // Implementation of StubCodeDesc aoqi@0: aoqi@0: StubCodeDesc* StubCodeDesc::_list = NULL; aoqi@0: int StubCodeDesc::_count = 0; aoqi@0: aoqi@0: aoqi@0: StubCodeDesc* StubCodeDesc::desc_for(address pc) { aoqi@0: StubCodeDesc* p = _list; aoqi@0: while (p != NULL && !p->contains(pc)) p = p->_next; aoqi@0: // p == NULL || p->contains(pc) aoqi@0: return p; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: StubCodeDesc* StubCodeDesc::desc_for_index(int index) { aoqi@0: StubCodeDesc* p = _list; aoqi@0: while (p != NULL && p->index() != index) p = p->_next; aoqi@0: return p; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: const char* StubCodeDesc::name_for(address pc) { aoqi@0: StubCodeDesc* p = desc_for(pc); aoqi@0: return p == NULL ? NULL : p->name(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void StubCodeDesc::print_on(outputStream* st) const { aoqi@0: st->print("%s", group()); aoqi@0: st->print("::"); aoqi@0: st->print("%s", name()); aoqi@0: st->print(" [" INTPTR_FORMAT ", " INTPTR_FORMAT "[ (%d bytes)", p2i(begin()), p2i(end()), size_in_bytes()); aoqi@0: } aoqi@0: aoqi@0: // Implementation of StubCodeGenerator aoqi@0: aoqi@0: StubCodeGenerator::StubCodeGenerator(CodeBuffer* code, bool print_code) { aoqi@0: _masm = new MacroAssembler(code); aoqi@0: _first_stub = _last_stub = NULL; aoqi@0: _print_code = print_code; aoqi@0: } aoqi@0: aoqi@0: extern "C" { aoqi@0: static int compare_cdesc(const void* void_a, const void* void_b) { aoqi@0: int ai = (*((StubCodeDesc**) void_a))->index(); aoqi@0: int bi = (*((StubCodeDesc**) void_b))->index(); aoqi@0: return ai - bi; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: StubCodeGenerator::~StubCodeGenerator() { aoqi@0: if (PrintStubCode || _print_code) { aoqi@0: CodeBuffer* cbuf = _masm->code(); aoqi@0: CodeBlob* blob = CodeCache::find_blob_unsafe(cbuf->insts()->start()); aoqi@0: if (blob != NULL) { aoqi@0: blob->set_strings(cbuf->strings()); aoqi@0: } aoqi@0: bool saw_first = false; aoqi@0: StubCodeDesc* toprint[1000]; aoqi@0: int toprint_len = 0; aoqi@0: for (StubCodeDesc* cdesc = _last_stub; cdesc != NULL; cdesc = cdesc->_next) { aoqi@0: toprint[toprint_len++] = cdesc; aoqi@0: if (cdesc == _first_stub) { saw_first = true; break; } aoqi@0: } aoqi@0: assert(saw_first, "must get both first & last"); aoqi@0: // Print in reverse order: aoqi@0: qsort(toprint, toprint_len, sizeof(toprint[0]), compare_cdesc); aoqi@0: for (int i = 0; i < toprint_len; i++) { aoqi@0: StubCodeDesc* cdesc = toprint[i]; aoqi@0: cdesc->print(); aoqi@0: tty->cr(); aoqi@0: Disassembler::decode(cdesc->begin(), cdesc->end()); aoqi@0: tty->cr(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void StubCodeGenerator::stub_prolog(StubCodeDesc* cdesc) { aoqi@0: // default implementation - do nothing aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void StubCodeGenerator::stub_epilog(StubCodeDesc* cdesc) { aoqi@0: // default implementation - record the cdesc aoqi@0: if (_first_stub == NULL) _first_stub = cdesc; aoqi@0: _last_stub = cdesc; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Implementation of CodeMark aoqi@0: aoqi@0: StubCodeMark::StubCodeMark(StubCodeGenerator* cgen, const char* group, const char* name) { aoqi@0: _cgen = cgen; aoqi@0: _cdesc = new StubCodeDesc(group, name, _cgen->assembler()->pc()); aoqi@0: _cgen->stub_prolog(_cdesc); aoqi@0: // define the stub's beginning (= entry point) to be after the prolog: aoqi@0: _cdesc->set_begin(_cgen->assembler()->pc()); aoqi@0: } aoqi@0: aoqi@0: StubCodeMark::~StubCodeMark() { aoqi@0: _cgen->assembler()->flush(); aoqi@0: _cdesc->set_end(_cgen->assembler()->pc()); aoqi@0: assert(StubCodeDesc::_list == _cdesc, "expected order on list"); aoqi@0: _cgen->stub_epilog(_cdesc); aoqi@0: Forte::register_stub(_cdesc->name(), _cdesc->begin(), _cdesc->end()); aoqi@0: aoqi@0: if (JvmtiExport::should_post_dynamic_code_generated()) { aoqi@0: JvmtiExport::post_dynamic_code_generated(_cdesc->name(), _cdesc->begin(), _cdesc->end()); aoqi@0: } aoqi@0: }