duke@435: /* never@1999: * Copyright (c) 1997, 2010, Oracle and/or its affiliates. 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: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: duke@435: #include "incls/_precompiled.incl" duke@435: #include "incls/_stubCodeGenerator.cpp.incl" duke@435: duke@435: duke@435: // Implementation of StubCodeDesc duke@435: duke@435: StubCodeDesc* StubCodeDesc::_list = NULL; duke@435: int StubCodeDesc::_count = 0; duke@435: duke@435: duke@435: StubCodeDesc* StubCodeDesc::desc_for(address pc) { duke@435: StubCodeDesc* p = _list; duke@435: while (p != NULL && !p->contains(pc)) p = p->_next; duke@435: // p == NULL || p->contains(pc) duke@435: return p; duke@435: } duke@435: duke@435: duke@435: StubCodeDesc* StubCodeDesc::desc_for_index(int index) { duke@435: StubCodeDesc* p = _list; duke@435: while (p != NULL && p->index() != index) p = p->_next; duke@435: return p; duke@435: } duke@435: duke@435: duke@435: const char* StubCodeDesc::name_for(address pc) { duke@435: StubCodeDesc* p = desc_for(pc); duke@435: return p == NULL ? NULL : p->name(); duke@435: } duke@435: duke@435: duke@435: void StubCodeDesc::print() { duke@435: tty->print(group()); duke@435: tty->print("::"); duke@435: tty->print(name()); duke@435: tty->print(" [" INTPTR_FORMAT ", " INTPTR_FORMAT "[ (%d bytes)", begin(), end(), size_in_bytes()); duke@435: } duke@435: duke@435: duke@435: duke@435: // Implementation of StubCodeGenerator duke@435: duke@435: StubCodeGenerator::StubCodeGenerator(CodeBuffer* code) { duke@435: _masm = new MacroAssembler(code); duke@435: _first_stub = _last_stub = NULL; duke@435: } duke@435: duke@435: extern "C" { duke@435: static int compare_cdesc(const void* void_a, const void* void_b) { duke@435: int ai = (*((StubCodeDesc**) void_a))->index(); duke@435: int bi = (*((StubCodeDesc**) void_b))->index(); duke@435: return ai - bi; duke@435: } duke@435: } duke@435: duke@435: StubCodeGenerator::~StubCodeGenerator() { duke@435: if (PrintStubCode) { duke@435: CodeBuffer* cbuf = _masm->code(); duke@435: CodeBlob* blob = CodeCache::find_blob_unsafe(cbuf->insts()->start()); duke@435: if (blob != NULL) { duke@435: blob->set_comments(cbuf->comments()); duke@435: } duke@435: bool saw_first = false; duke@435: StubCodeDesc* toprint[1000]; duke@435: int toprint_len = 0; duke@435: for (StubCodeDesc* cdesc = _last_stub; cdesc != NULL; cdesc = cdesc->_next) { duke@435: toprint[toprint_len++] = cdesc; duke@435: if (cdesc == _first_stub) { saw_first = true; break; } duke@435: } duke@435: assert(saw_first, "must get both first & last"); duke@435: // Print in reverse order: duke@435: qsort(toprint, toprint_len, sizeof(toprint[0]), compare_cdesc); duke@435: for (int i = 0; i < toprint_len; i++) { duke@435: StubCodeDesc* cdesc = toprint[i]; duke@435: cdesc->print(); duke@435: tty->cr(); duke@435: Disassembler::decode(cdesc->begin(), cdesc->end()); duke@435: tty->cr(); duke@435: } duke@435: } duke@435: } duke@435: duke@435: duke@435: void StubCodeGenerator::stub_prolog(StubCodeDesc* cdesc) { duke@435: // default implementation - do nothing duke@435: } duke@435: duke@435: duke@435: void StubCodeGenerator::stub_epilog(StubCodeDesc* cdesc) { duke@435: // default implementation - record the cdesc duke@435: if (_first_stub == NULL) _first_stub = cdesc; duke@435: _last_stub = cdesc; duke@435: } duke@435: duke@435: duke@435: // Implementation of CodeMark duke@435: duke@435: StubCodeMark::StubCodeMark(StubCodeGenerator* cgen, const char* group, const char* name) { duke@435: _cgen = cgen; duke@435: _cdesc = new StubCodeDesc(group, name, _cgen->assembler()->pc()); duke@435: _cgen->stub_prolog(_cdesc); duke@435: // define the stub's beginning (= entry point) to be after the prolog: duke@435: _cdesc->set_begin(_cgen->assembler()->pc()); duke@435: } duke@435: duke@435: StubCodeMark::~StubCodeMark() { duke@435: _cgen->assembler()->flush(); duke@435: _cdesc->set_end(_cgen->assembler()->pc()); duke@435: assert(StubCodeDesc::_list == _cdesc, "expected order on list"); duke@435: _cgen->stub_epilog(_cdesc); duke@435: Forte::register_stub(_cdesc->name(), _cdesc->begin(), _cdesc->end()); duke@435: duke@435: if (JvmtiExport::should_post_dynamic_code_generated()) { duke@435: JvmtiExport::post_dynamic_code_generated(_cdesc->name(), _cdesc->begin(), _cdesc->end()); duke@435: } duke@435: }