1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "asm/macroAssembler.hpp"
  27 #include "asm/macroAssembler.inline.hpp"
  28 #include "code/codeCache.hpp"
  29 #include "code/codeCacheExtensions.hpp"
  30 #include "compiler/disassembler.hpp"
  31 #include "oops/oop.inline.hpp"
  32 #include "prims/forte.hpp"
  33 #include "runtime/stubCodeGenerator.hpp"
  34 
  35 
  36 // Implementation of StubCodeDesc
  37 
  38 StubCodeDesc* StubCodeDesc::_list = NULL;
  39 bool          StubCodeDesc::_frozen = false;
  40 
  41 StubCodeDesc* StubCodeDesc::desc_for(address pc) {
  42   StubCodeDesc* p = _list;
  43   while (p != NULL && !p->contains(pc)) {
  44     p = p->_next;
  45   }
  46   return p;
  47 }
  48 
  49 const char* StubCodeDesc::name_for(address pc) {
  50   StubCodeDesc* p = desc_for(pc);
  51   return p == NULL ? NULL : p->name();
  52 }
  53 
  54 
  55 void StubCodeDesc::freeze() {
  56   assert(!_frozen, "repeated freeze operation");
  57   _frozen = true;
  58 }
  59 
  60 void StubCodeDesc::print_on(outputStream* st) const {
  61   st->print("%s", group());
  62   st->print("::");
  63   st->print("%s", name());
  64   st->print(" [" INTPTR_FORMAT ", " INTPTR_FORMAT "[ (%d bytes)", p2i(begin()), p2i(end()), size_in_bytes());
  65 }
  66 
  67 // Implementation of StubCodeGenerator
  68 
  69 StubCodeGenerator::StubCodeGenerator(CodeBuffer* code, bool print_code) {
  70   _masm = new MacroAssembler(code );
  71   _print_code = PrintStubCode || print_code;
  72 }
  73 
  74 StubCodeGenerator::~StubCodeGenerator() {
  75   if (_print_code) {
  76     CodeBuffer* cbuf = _masm->code();
  77     CodeBlob*   blob = CodeCache::find_blob_unsafe(cbuf->insts()->start());
  78     if (blob != NULL) {
  79       blob->set_strings(cbuf->strings());
  80     }
  81   }
  82 }
  83 
  84 void StubCodeGenerator::stub_prolog(StubCodeDesc* cdesc) {
  85   // default implementation - do nothing
  86 }
  87 
  88 void StubCodeGenerator::stub_epilog(StubCodeDesc* cdesc) {
  89   if (_print_code) {
  90     cdesc->print();
  91     tty->cr();
  92     Disassembler::decode(cdesc->begin(), cdesc->end());
  93     tty->cr();
  94   }
  95 }
  96 
  97 
  98 // Implementation of CodeMark
  99 
 100 StubCodeMark::StubCodeMark(StubCodeGenerator* cgen, const char* group, const char* name) {
 101   _cgen  = cgen;
 102   _cdesc = new StubCodeDesc(group, name, _cgen->assembler()->pc());
 103   _cgen->stub_prolog(_cdesc);
 104   // define the stub's beginning (= entry point) to be after the prolog:
 105   _cdesc->set_begin(_cgen->assembler()->pc());
 106 }
 107 
 108 StubCodeMark::~StubCodeMark() {
 109   _cgen->assembler()->flush();
 110   _cdesc->set_end(_cgen->assembler()->pc());
 111   assert(StubCodeDesc::_list == _cdesc, "expected order on list");
 112   _cgen->stub_epilog(_cdesc);
 113   Forte::register_stub(_cdesc->name(), _cdesc->begin(), _cdesc->end());
 114 
 115   if (JvmtiExport::should_post_dynamic_code_generated()) {
 116     JvmtiExport::post_dynamic_code_generated(_cdesc->name(), _cdesc->begin(), _cdesc->end());
 117   }
 118 }