1 /*
   2  * Copyright (c) 1997, 2010, 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 "compiler/disassembler.hpp"
  27 #include "oops/oop.inline.hpp"
  28 #include "prims/forte.hpp"
  29 #include "runtime/stubCodeGenerator.hpp"
  30 #ifdef TARGET_ARCH_x86
  31 # include "assembler_x86.inline.hpp"
  32 #endif
  33 #ifdef TARGET_ARCH_sparc
  34 # include "assembler_sparc.inline.hpp"
  35 #endif
  36 #ifdef TARGET_ARCH_zero
  37 # include "assembler_zero.inline.hpp"
  38 #endif
  39 
  40 
  41 // Implementation of StubCodeDesc
  42 
  43 StubCodeDesc* StubCodeDesc::_list = NULL;
  44 int           StubCodeDesc::_count = 0;
  45 
  46 
  47 StubCodeDesc* StubCodeDesc::desc_for(address pc) {
  48   StubCodeDesc* p = _list;
  49   while (p != NULL && !p->contains(pc)) p = p->_next;
  50   // p == NULL || p->contains(pc)
  51   return p;
  52 }
  53 
  54 
  55 StubCodeDesc* StubCodeDesc::desc_for_index(int index) {
  56   StubCodeDesc* p = _list;
  57   while (p != NULL && p->index() != index) p = p->_next;
  58   return p;
  59 }
  60 
  61 
  62 const char* StubCodeDesc::name_for(address pc) {
  63   StubCodeDesc* p = desc_for(pc);
  64   return p == NULL ? NULL : p->name();
  65 }
  66 
  67 
  68 void StubCodeDesc::print_on(outputStream* st) const {
  69   st->print(group());
  70   st->print("::");
  71   st->print(name());
  72   st->print(" [" INTPTR_FORMAT ", " INTPTR_FORMAT "[ (%d bytes)", begin(), end(), size_in_bytes());
  73 }
  74 
  75 // Implementation of StubCodeGenerator
  76 
  77 StubCodeGenerator::StubCodeGenerator(CodeBuffer* code) {
  78   _masm = new MacroAssembler(code);
  79   _first_stub = _last_stub = NULL;
  80 }
  81 
  82 extern "C" {
  83   static int compare_cdesc(const void* void_a, const void* void_b) {
  84     int ai = (*((StubCodeDesc**) void_a))->index();
  85     int bi = (*((StubCodeDesc**) void_b))->index();
  86     return ai - bi;
  87   }
  88 }
  89 
  90 StubCodeGenerator::~StubCodeGenerator() {
  91   if (PrintStubCode) {
  92     CodeBuffer* cbuf = _masm->code();
  93     CodeBlob*   blob = CodeCache::find_blob_unsafe(cbuf->insts()->start());
  94     if (blob != NULL) {
  95       blob->set_comments(cbuf->comments());
  96     }
  97     bool saw_first = false;
  98     StubCodeDesc* toprint[1000];
  99     int toprint_len = 0;
 100     for (StubCodeDesc* cdesc = _last_stub; cdesc != NULL; cdesc = cdesc->_next) {
 101       toprint[toprint_len++] = cdesc;
 102       if (cdesc == _first_stub) { saw_first = true; break; }
 103     }
 104     assert(saw_first, "must get both first & last");
 105     // Print in reverse order:
 106     qsort(toprint, toprint_len, sizeof(toprint[0]), compare_cdesc);
 107     for (int i = 0; i < toprint_len; i++) {
 108       StubCodeDesc* cdesc = toprint[i];
 109       cdesc->print();
 110       tty->cr();
 111       Disassembler::decode(cdesc->begin(), cdesc->end());
 112       tty->cr();
 113     }
 114   }
 115 }
 116 
 117 
 118 void StubCodeGenerator::stub_prolog(StubCodeDesc* cdesc) {
 119   // default implementation - do nothing
 120 }
 121 
 122 
 123 void StubCodeGenerator::stub_epilog(StubCodeDesc* cdesc) {
 124   // default implementation - record the cdesc
 125   if (_first_stub == NULL)  _first_stub = cdesc;
 126   _last_stub = cdesc;
 127 }
 128 
 129 
 130 // Implementation of CodeMark
 131 
 132 StubCodeMark::StubCodeMark(StubCodeGenerator* cgen, const char* group, const char* name) {
 133   _cgen  = cgen;
 134   _cdesc = new StubCodeDesc(group, name, _cgen->assembler()->pc());
 135   _cgen->stub_prolog(_cdesc);
 136   // define the stub's beginning (= entry point) to be after the prolog:
 137   _cdesc->set_begin(_cgen->assembler()->pc());
 138 }
 139 
 140 StubCodeMark::~StubCodeMark() {
 141   _cgen->assembler()->flush();
 142   _cdesc->set_end(_cgen->assembler()->pc());
 143   assert(StubCodeDesc::_list == _cdesc, "expected order on list");
 144   _cgen->stub_epilog(_cdesc);
 145   Forte::register_stub(_cdesc->name(), _cdesc->begin(), _cdesc->end());
 146 
 147   if (JvmtiExport::should_post_dynamic_code_generated()) {
 148     JvmtiExport::post_dynamic_code_generated(_cdesc->name(), _cdesc->begin(), _cdesc->end());
 149   }
 150 }